1/* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27#include "config.h"
28#include "system.h"
29#include "coretypes.h"
30#include "cp-tree.h"
31#include "timevar.h"
32#include "stringpool.h"
33#include "varasm.h"
34#include "attribs.h"
35#include "stor-layout.h"
36#include "intl.h"
37#include "c-family/c-objc.h"
38#include "cp-objcp-common.h"
39#include "toplev.h"
40#include "tree-iterator.h"
41#include "type-utils.h"
42#include "gimplify.h"
43
44/* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46typedef int (*tree_fn_t) (tree, void*);
47
48/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
54};
55
56static GTY(()) struct pending_template *pending_templates;
57static GTY(()) struct pending_template *last_pending_template;
58
59int processing_template_parmlist;
60static int template_header_count;
61
62static GTY(()) tree saved_trees;
63static vec<int> inline_parm_levels;
64
65static GTY(()) struct tinst_level *current_tinst_level;
66
67static GTY(()) tree saved_access_scope;
68
69/* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72static tree cur_stmt_expr;
73
74// -------------------------------------------------------------------------- //
75// Local Specialization Stack
76//
77// Implementation of the RAII helper for creating new local
78// specializations.
79local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
81{
82 local_specializations = new hash_map<tree, tree>;
83}
84
85local_specialization_stack::~local_specialization_stack ()
86{
87 delete local_specializations;
88 local_specializations = saved;
89}
90
91/* True if we've recursed into fn_type_unification too many times. */
92static bool excessive_deduction_depth;
93
94struct GTY((for_user)) spec_entry
95{
96 tree tmpl;
97 tree args;
98 tree spec;
99};
100
101struct spec_hasher : ggc_ptr_hash<spec_entry>
102{
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
105};
106
107static GTY (()) hash_table<spec_hasher> *decl_specializations;
108
109static GTY (()) hash_table<spec_hasher> *type_specializations;
110
111/* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115static GTY(()) vec<tree, va_gc> *canonical_template_parms;
116
117#define UNIFY_ALLOW_NONE 0
118#define UNIFY_ALLOW_MORE_CV_QUAL 1
119#define UNIFY_ALLOW_LESS_CV_QUAL 2
120#define UNIFY_ALLOW_DERIVED 4
121#define UNIFY_ALLOW_INTEGER 8
122#define UNIFY_ALLOW_OUTER_LEVEL 16
123#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
125
126enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
130};
131
132static void push_access_scope (tree);
133static void pop_access_scope (tree);
134static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139static int unify (tree, tree, tree, tree, int, bool);
140static void add_pending_template (tree);
141static tree reopen_tinst_level (struct tinst_level *);
142static tree tsubst_initializer_list (tree, tree);
143static tree get_partial_spec_bindings (tree, tree, tree);
144static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148static void tsubst_enum (tree, tree, tree);
149static tree add_to_template_args (tree, tree);
150static tree add_outermost_template_args (tree, tree);
151static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158static void note_template_header (int);
159static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165static tree expand_template_argument_pack (tree);
166static tree build_template_parm_index (int, int, int, tree, tree);
167static bool inline_needs_template_parms (tree, bool);
168static void push_inline_template_parms_recursive (tree, int);
169static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170static int mark_template_parm (tree, void *);
171static int template_parm_this_level_p (tree, void *);
172static tree tsubst_friend_function (tree, tree);
173static tree tsubst_friend_class (tree, tree);
174static int can_complete_type_without_circularity (tree);
175static tree get_bindings (tree, tree, tree, bool);
176static int template_decl_level (tree);
177static int check_cv_quals_for_unify (int, tree, tree);
178static void template_parm_level_and_index (tree, int*, int*);
179static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181static tree copy_template_args (tree);
182static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189static bool check_specialization_scope (void);
190static tree process_partial_specialization (tree);
191static void set_current_access_from_decl (tree);
192static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194static tree try_class_unification (tree, tree, tree, tree, bool);
195static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197static bool template_template_parm_bindings_ok_p (tree, tree);
198static void tsubst_default_arguments (tree, tsubst_flags_t);
199static tree for_each_template_parm_r (tree *, int *, void *);
200static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201static void copy_default_args_to_explicit_spec (tree);
202static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203static bool dependent_template_arg_p (tree);
204static bool any_template_arguments_need_structural_equality_p (tree);
205static bool dependent_type_p_r (tree);
206static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207static tree tsubst_decl (tree, tree, tsubst_flags_t);
208static void perform_typedefs_access_check (tree tmpl, tree targs);
209static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211static tree listify (tree);
212static tree listify_autos (tree, tree);
213static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215static bool complex_alias_template_p (const_tree tmpl);
216static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218
219/* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
223
224static void
225push_access_scope (tree t)
226{
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
229
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
236
237 if (TREE_CODE (t) == FUNCTION_DECL)
238 {
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
242 }
243}
244
245/* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
247
248static void
249pop_access_scope (tree t)
250{
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
255 }
256
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
261}
262
263/* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
267
268tree
269finish_member_template_decl (tree decl)
270{
271 if (decl == error_mark_node)
272 return error_mark_node;
273
274 gcc_assert (DECL_P (decl));
275
276 if (TREE_CODE (decl) == TYPE_DECL)
277 {
278 tree type;
279
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 {
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
290 }
291 return NULL_TREE;
292 }
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
296 {
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 {
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
301 }
302 else
303 return decl;
304 }
305 else
306 error ("invalid member template declaration %qD", decl);
307
308 return error_mark_node;
309}
310
311/* Create a template info node. */
312
313tree
314build_template_info (tree template_decl, tree template_args)
315{
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
320}
321
322/* Return the template info node corresponding to T, whatever T is. */
323
324tree
325get_template_info (const_tree t)
326{
327 tree tinfo = NULL_TREE;
328
329 if (!t || t == error_mark_node)
330 return NULL;
331
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
335
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
338
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
341
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
346
347 return tinfo;
348}
349
350/* Returns the template nesting level of the indicated class TYPE.
351
352 For example, in:
353 template <class T>
354 struct A
355 {
356 template <class U>
357 struct B {};
358 };
359
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
363
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
367
368int
369template_class_depth (tree type)
370{
371 int depth;
372
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
374 {
375 tree tinfo = get_template_info (type);
376
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
380
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
387 }
388
389 return depth;
390}
391
392/* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
394
395static bool
396inline_needs_template_parms (tree decl, bool nsdmi)
397{
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
400
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
403}
404
405/* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
409
410static void
411push_inline_template_parms_recursive (tree parmlist, int levels)
412{
413 tree parms = TREE_VALUE (parmlist);
414 int i;
415
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
418
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
424
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
428 {
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
430
431 if (error_operand_p (parm))
432 continue;
433
434 gcc_assert (DECL_P (parm));
435
436 switch (TREE_CODE (parm))
437 {
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
442
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
447
448 default:
449 gcc_unreachable ();
450 }
451 }
452}
453
454/* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
457
458void
459maybe_begin_member_template_processing (tree decl)
460{
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
464
465 if (nsdmi)
466 {
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
472 }
473
474 if (inline_needs_template_parms (decl, nsdmi))
475 {
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
478
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
480 {
481 --levels;
482 parms = TREE_CHAIN (parms);
483 }
484
485 push_inline_template_parms_recursive (parms, levels);
486 }
487
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
491}
492
493/* Undo the effects of maybe_begin_member_template_processing. */
494
495void
496maybe_end_member_template_processing (void)
497{
498 int i;
499 int last;
500
501 if (inline_parm_levels.length () == 0)
502 return;
503
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
506 {
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
510 }
511}
512
513/* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
515
516static tree
517add_to_template_args (tree args, tree extra_args)
518{
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
523
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
526
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
529
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
532
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
535
536 return new_args;
537}
538
539/* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
545
546static tree
547add_outermost_template_args (tree args, tree extra_args)
548{
549 tree new_args;
550
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
554
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
559
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
562
563 new_args = add_to_template_args (args, extra_args);
564
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
567
568 return new_args;
569}
570
571/* Return the N levels of innermost template arguments from the ARGS. */
572
573tree
574get_innermost_template_args (tree args, int n)
575{
576 tree new_args;
577 int extra_levels;
578 int i;
579
580 gcc_assert (n >= 0);
581
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
585
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
592
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
598
599 return new_args;
600}
601
602/* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
604
605static tree
606strip_innermost_template_args (tree args, int extra_levels)
607{
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
611
612 gcc_assert (n >= 0);
613
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
617
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
623
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
629
630 return new_args;
631}
632
633/* We've got a template header coming up; push to a new level for storing
634 the parms. */
635
636void
637begin_template_parm_list (void)
638{
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
646
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
649 };
650
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
657
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
663}
664
665/* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
668
669static bool
670check_specialization_scope (void)
671{
672 tree scope = current_scope ();
673
674 /* [temp.expl.spec]
675
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
684 {
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
687 }
688
689 /* [temp.expl.spec]
690
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
698 {
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
701 }
702
703 return true;
704}
705
706/* We've just seen template <>. */
707
708bool
709begin_specialization (void)
710{
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
714}
715
716/* Called at then end of processing a declaration preceded by
717 template<>. */
718
719void
720end_specialization (void)
721{
722 finish_scope ();
723 reset_specialization ();
724}
725
726/* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
728
729void
730reset_specialization (void)
731{
732 processing_specialization = 0;
733 template_header_count = 0;
734}
735
736/* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
738
739static void
740note_template_header (int specialization)
741{
742 processing_specialization = specialization;
743 template_header_count++;
744}
745
746/* We're beginning an explicit instantiation. */
747
748void
749begin_explicit_instantiation (void)
750{
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
753}
754
755
756void
757end_explicit_instantiation (void)
758{
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
761}
762
763/* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
767
768static bool
769check_specialization_namespace (tree tmpl)
770{
771 tree tpl_ns = decl_namespace_context (tmpl);
772
773 /* [tmpl.expl.spec]
774
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
780 if (current_scope() != DECL_CONTEXT (tmpl)
781 && !at_namespace_scope_p ())
782 {
783 error ("specialization of %qD must appear at namespace scope", tmpl);
784 return false;
785 }
786
787 if (cxx_dialect < cxx11
788 ? is_associated_namespace (current_namespace, tpl_ns)
789 : is_ancestor (current_namespace, tpl_ns))
790 /* Same or enclosing namespace. */
791 return true;
792 else
793 {
794 permerror (input_location,
795 "specialization of %qD in different namespace", tmpl);
796 inform (DECL_SOURCE_LOCATION (tmpl),
797 " from definition of %q#D", tmpl);
798 return false;
799 }
800}
801
802/* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
804
805static void
806check_explicit_instantiation_namespace (tree spec)
807{
808 tree ns;
809
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns = decl_namespace_context (spec);
813 if (!is_ancestor (current_namespace, ns))
814 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec, current_namespace, ns);
817}
818
819// Returns the type of a template specialization only if that
820// specialization needs to be defined. Otherwise (e.g., if the type has
821// already been defined), the function returns NULL_TREE.
822static tree
823maybe_new_partial_specialization (tree type)
824{
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
827 //
828 // template<typename T>
829 // struct S;
830 //
831 // template<typename T>
832 // struct S<T*>;
833 //
834 // Here, S<T*> is an implicit instantiation of S whose type
835 // is incomplete.
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
837 return type;
838
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
841 //
842 // template<typename T>
843 // requires Integral<T>
844 // struct S<T*>;
845 //
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
851 //
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
856 {
857 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 tree args = CLASSTYPE_TI_ARGS (type);
859
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms)
863 return NULL_TREE;
864
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
867 return NULL_TREE;
868
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr = current_template_constraints ();
872
873 if (type == TREE_TYPE (tmpl))
874 {
875 tree main_constr = get_constraints (tmpl);
876 if (equivalent_constraints (type_constr, main_constr))
877 return NULL_TREE;
878 }
879
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
883 while (specs)
884 {
885 tree spec_tmpl = TREE_VALUE (specs);
886 tree spec_args = TREE_PURPOSE (specs);
887 tree spec_constr = get_constraints (spec_tmpl);
888 if (comp_template_args (args, spec_args)
889 && equivalent_constraints (type_constr, spec_constr))
890 return NULL_TREE;
891 specs = TREE_CHAIN (specs);
892 }
893
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t = make_class_type (TREE_CODE (type));
897 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
898 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
899
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
904
905 // Build the corresponding type decl.
906 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
907 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
908 DECL_SOURCE_LOCATION (d) = input_location;
909
910 return t;
911 }
912
913 return NULL_TREE;
914}
915
916/* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
918
919tree
920maybe_process_partial_specialization (tree type)
921{
922 tree context;
923
924 if (type == error_mark_node)
925 return error_mark_node;
926
927 /* A lambda that appears in specialization context is not itself a
928 specialization. */
929 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
930 return type;
931
932 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
933 {
934 error ("name of class shadows template template parameter %qD",
935 TYPE_NAME (type));
936 return error_mark_node;
937 }
938
939 context = TYPE_CONTEXT (type);
940
941 if (TYPE_ALIAS_P (type))
942 {
943 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
944
945 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
946 error ("specialization of alias template %qD",
947 TI_TEMPLATE (tinfo));
948 else
949 error ("explicit specialization of non-template %qT", type);
950 return error_mark_node;
951 }
952 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
953 {
954 /* This is for ordinary explicit specialization and partial
955 specialization of a template class such as:
956
957 template <> class C<int>;
958
959 or:
960
961 template <class T> class C<T*>;
962
963 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964
965 if (tree t = maybe_new_partial_specialization (type))
966 {
967 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
968 && !at_namespace_scope_p ())
969 return error_mark_node;
970 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
971 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
972 if (processing_template_decl)
973 {
974 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
975 if (decl == error_mark_node)
976 return error_mark_node;
977 return TREE_TYPE (decl);
978 }
979 }
980 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
981 error ("specialization of %qT after instantiation", type);
982 else if (errorcount && !processing_specialization
983 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
984 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
985 /* Trying to define a specialization either without a template<> header
986 or in an inappropriate place. We've already given an error, so just
987 bail now so we don't actually define the specialization. */
988 return error_mark_node;
989 }
990 else if (CLASS_TYPE_P (type)
991 && !CLASSTYPE_USE_TEMPLATE (type)
992 && CLASSTYPE_TEMPLATE_INFO (type)
993 && context && CLASS_TYPE_P (context)
994 && CLASSTYPE_TEMPLATE_INFO (context))
995 {
996 /* This is for an explicit specialization of member class
997 template according to [temp.expl.spec/18]:
998
999 template <> template <class U> class C<int>::D;
1000
1001 The context `C<int>' must be an implicit instantiation.
1002 Otherwise this is just a member class template declared
1003 earlier like:
1004
1005 template <> class C<int> { template <class U> class D; };
1006 template <> template <class U> class C<int>::D;
1007
1008 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1009 while in the second case, `C<int>::D' is a primary template
1010 and `C<T>::D' may not exist. */
1011
1012 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1013 && !COMPLETE_TYPE_P (type))
1014 {
1015 tree t;
1016 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1017
1018 if (current_namespace
1019 != decl_namespace_context (tmpl))
1020 {
1021 permerror (input_location,
1022 "specializing %q#T in different namespace", type);
1023 permerror (DECL_SOURCE_LOCATION (tmpl),
1024 " from definition of %q#D", tmpl);
1025 }
1026
1027 /* Check for invalid specialization after instantiation:
1028
1029 template <> template <> class C<int>::D<int>;
1030 template <> template <class U> class C<int>::D; */
1031
1032 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1033 t; t = TREE_CHAIN (t))
1034 {
1035 tree inst = TREE_VALUE (t);
1036 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1037 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1038 {
1039 /* We already have a full specialization of this partial
1040 instantiation, or a full specialization has been
1041 looked up but not instantiated. Reassign it to the
1042 new member specialization template. */
1043 spec_entry elt;
1044 spec_entry *entry;
1045
1046 elt.tmpl = most_general_template (tmpl);
1047 elt.args = CLASSTYPE_TI_ARGS (inst);
1048 elt.spec = inst;
1049
1050 type_specializations->remove_elt (&elt);
1051
1052 elt.tmpl = tmpl;
1053 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1054
1055 spec_entry **slot
1056 = type_specializations->find_slot (&elt, INSERT);
1057 entry = ggc_alloc<spec_entry> ();
1058 *entry = elt;
1059 *slot = entry;
1060 }
1061 else
1062 /* But if we've had an implicit instantiation, that's a
1063 problem ([temp.expl.spec]/6). */
1064 error ("specialization %qT after instantiation %qT",
1065 type, inst);
1066 }
1067
1068 /* Mark TYPE as a specialization. And as a result, we only
1069 have one level of template argument for the innermost
1070 class template. */
1071 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1072 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1073 CLASSTYPE_TI_ARGS (type)
1074 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1075 }
1076 }
1077 else if (processing_specialization)
1078 {
1079 /* Someday C++0x may allow for enum template specialization. */
1080 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1081 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1082 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1083 "of %qD not allowed by ISO C++", type);
1084 else
1085 {
1086 error ("explicit specialization of non-template %qT", type);
1087 return error_mark_node;
1088 }
1089 }
1090
1091 return type;
1092}
1093
1094/* Returns nonzero if we can optimize the retrieval of specializations
1095 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1096 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097
1098static inline bool
1099optimize_specialization_lookup_p (tree tmpl)
1100{
1101 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1102 && DECL_CLASS_SCOPE_P (tmpl)
1103 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1104 parameter. */
1105 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1106 /* The optimized lookup depends on the fact that the
1107 template arguments for the member function template apply
1108 purely to the containing class, which is not true if the
1109 containing class is an explicit or partial
1110 specialization. */
1111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1112 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1113 && !DECL_CONV_FN_P (tmpl)
1114 /* It is possible to have a template that is not a member
1115 template and is not a member of a template class:
1116
1117 template <typename T>
1118 struct S { friend A::f(); };
1119
1120 Here, the friend function is a template, but the context does
1121 not have template information. The optimized lookup relies
1122 on having ARGS be the template arguments for both the class
1123 and the function template. */
1124 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1125}
1126
1127/* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1128 gone through coerce_template_parms by now. */
1129
1130static void
1131verify_unstripped_args (tree args)
1132{
1133 ++processing_template_decl;
1134 if (!any_dependent_template_arguments_p (args))
1135 {
1136 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1137 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1138 {
1139 tree arg = TREE_VEC_ELT (inner, i);
1140 if (TREE_CODE (arg) == TEMPLATE_DECL)
1141 /* OK */;
1142 else if (TYPE_P (arg))
1143 gcc_assert (strip_typedefs (arg, NULL) == arg);
1144 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1145 /* Allow typedefs on the type of a non-type argument, since a
1146 parameter can have them. */;
1147 else
1148 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1149 }
1150 }
1151 --processing_template_decl;
1152}
1153
1154/* Retrieve the specialization (in the sense of [temp.spec] - a
1155 specialization is either an instantiation or an explicit
1156 specialization) of TMPL for the given template ARGS. If there is
1157 no such specialization, return NULL_TREE. The ARGS are a vector of
1158 arguments, or a vector of vectors of arguments, in the case of
1159 templates with more than one level of parameters.
1160
1161 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1162 then we search for a partial specialization matching ARGS. This
1163 parameter is ignored if TMPL is not a class template.
1164
1165 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1166 result is a NONTYPE_ARGUMENT_PACK. */
1167
1168static tree
1169retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1170{
1171 if (tmpl == NULL_TREE)
1172 return NULL_TREE;
1173
1174 if (args == error_mark_node)
1175 return NULL_TREE;
1176
1177 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1178 || TREE_CODE (tmpl) == FIELD_DECL);
1179
1180 /* There should be as many levels of arguments as there are
1181 levels of parameters. */
1182 gcc_assert (TMPL_ARGS_DEPTH (args)
1183 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1185 : template_class_depth (DECL_CONTEXT (tmpl))));
1186
1187 if (flag_checking)
1188 verify_unstripped_args (args);
1189
1190 if (optimize_specialization_lookup_p (tmpl))
1191 {
1192 tree class_template;
1193 tree class_specialization;
1194 vec<tree, va_gc> *methods;
1195 tree fns;
1196 int idx;
1197
1198 /* The template arguments actually apply to the containing
1199 class. Find the class specialization with those
1200 arguments. */
1201 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1202 class_specialization
1203 = retrieve_specialization (class_template, args, 0);
1204 if (!class_specialization)
1205 return NULL_TREE;
1206 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1207 for the specialization. */
1208 idx = class_method_index_for_fn (class_specialization, tmpl);
1209 if (idx == -1)
1210 return NULL_TREE;
1211 /* Iterate through the methods with the indicated name, looking
1212 for the one that has an instance of TMPL. */
1213 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1214 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1215 {
1216 tree fn = OVL_CURRENT (fns);
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1222 }
1223 return NULL_TREE;
1224 }
1225 else
1226 {
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1230
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1234
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1239
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1245 }
1246
1247 return NULL_TREE;
1248}
1249
1250/* Like retrieve_specialization, but for local declarations. */
1251
1252tree
1253retrieve_local_specialization (tree tmpl)
1254{
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1257
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1260}
1261
1262/* Returns nonzero iff DECL is a specialization of TMPL. */
1263
1264int
1265is_specialization_of (tree decl, tree tmpl)
1266{
1267 tree t;
1268
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1276 }
1277 else
1278 {
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1287 }
1288
1289 return 0;
1290}
1291
1292/* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1294
1295bool
1296is_specialization_of_friend (tree decl, tree friend_decl)
1297{
1298 bool need_template = true;
1299 int template_depth;
1300
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1303
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1310 {
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1314 }
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1318
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1322
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1325
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1331
1332 For example, given a template friend declaration
1333
1334 template <class T> friend void A<T>::f();
1335
1336 the member function below is considered a friend
1337
1338 template <> struct A<int> {
1339 void f();
1340 };
1341
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1345
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 {
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1354
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1357
1358 and DECL's are
1359
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1362
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1371
1372 while (current_depth < template_depth)
1373 {
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 {
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1381 }
1382 context = TYPE_CONTEXT (context);
1383 }
1384
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 {
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1392
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1400 {
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1409
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 }
1412 else
1413 decl_type = TREE_TYPE (decl);
1414
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1419
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1423
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1432
1433 return compparms (decl_args_type, friend_args_type);
1434 }
1435 else
1436 {
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1440
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1450 {
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1457
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1465 }
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1469 }
1470 }
1471 return false;
1472}
1473
1474/* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1478
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1481
1482static tree
1483register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1485{
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1489
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1493
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1502
1503 template <class T> void f(T t1, T t = T())
1504
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1508
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1514 {
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1518
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1521
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1528 }
1529
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1538 {
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1540 {
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1543 {
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1547 }
1548 else
1549 {
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1558
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1562
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1566
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1570
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1578
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1582 is.
1583
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1587 {
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1594 }
1595 check_specialization_namespace (tmpl);
1596
1597 return fn;
1598 }
1599 }
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1601 {
1602 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1606
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1612
1613 return fn;
1614 }
1615 }
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1618
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1624
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1626 {
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1638
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 }
1645
1646 return spec;
1647}
1648
1649/* Returns true iff two spec_entry nodes are equivalent. */
1650
1651int comparing_specializations;
1652
1653bool
1654spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1655{
1656 int equal;
1657
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1666 {
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1672 }
1673 --comparing_specializations;
1674
1675 return equal;
1676}
1677
1678/* Returns a hash for a template TMPL and template arguments ARGS. */
1679
1680static hashval_t
1681hash_tmpl_and_args (tree tmpl, tree args)
1682{
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1685}
1686
1687/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1689
1690hashval_t
1691spec_hasher::hash (spec_entry *e)
1692{
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1694}
1695
1696/* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1698
1699hashval_t
1700iterative_hash_template_arg (tree arg, hashval_t val)
1701{
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1705
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1708
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1711
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1714
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1717
1718 val = iterative_hash_object (code, val);
1719
1720 switch (code)
1721 {
1722 case ERROR_MARK:
1723 return val;
1724
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1727
1728 case TREE_VEC:
1729 {
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1734 }
1735
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1740
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1744
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1749
1750 case OVERLOAD:
1751 for (; arg; arg = OVL_NEXT (arg))
1752 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1753 return val;
1754
1755 case CONSTRUCTOR:
1756 {
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1760 {
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1763 }
1764 return val;
1765 }
1766
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1769 {
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1772 }
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1774
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1777
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1781
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1787
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1792
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1798
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1804
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1810
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1821
1822 default:
1823 break;
1824 }
1825
1826 switch (tclass)
1827 {
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1830 {
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1840 }
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1848
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1852
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1855 {
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1860 }
1861 }
1862 gcc_unreachable ();
1863 return 0;
1864}
1865
1866/* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1869
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1871
1872bool
1873reregister_specialization (tree spec, tree tinfo, tree new_spec)
1874{
1875 spec_entry *entry;
1876 spec_entry elt;
1877
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1881
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1884 {
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1889 }
1890
1891 return 0;
1892}
1893
1894/* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1896
1897void
1898register_local_specialization (tree spec, tree tmpl)
1899{
1900 local_specializations->put (tmpl, spec);
1901}
1902
1903/* TYPE is a class type. Returns true if TYPE is an explicitly
1904 specialized class. */
1905
1906bool
1907explicit_class_specialization_p (tree type)
1908{
1909 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1910 return false;
1911 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1912}
1913
1914/* Print the list of functions at FNS, going through all the overloads
1915 for each element of the list. Alternatively, FNS can not be a
1916 TREE_LIST, in which case it will be printed together with all the
1917 overloads.
1918
1919 MORE and *STR should respectively be FALSE and NULL when the function
1920 is called from the outside. They are used internally on recursive
1921 calls. print_candidates manages the two parameters and leaves NULL
1922 in *STR when it ends. */
1923
1924static void
1925print_candidates_1 (tree fns, bool more, const char **str)
1926{
1927 tree fn, fn2;
1928 char *spaces = NULL;
1929
1930 for (fn = fns; fn; fn = OVL_NEXT (fn))
1931 if (TREE_CODE (fn) == TREE_LIST)
1932 {
1933 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1934 print_candidates_1 (TREE_VALUE (fn2),
1935 TREE_CHAIN (fn2) || more, str);
1936 }
1937 else
1938 {
1939 tree cand = OVL_CURRENT (fn);
1940 if (!*str)
1941 {
1942 /* Pick the prefix string. */
1943 if (!more && !OVL_NEXT (fns))
1944 {
1945 inform (DECL_SOURCE_LOCATION (cand),
1946 "candidate is: %#D", cand);
1947 continue;
1948 }
1949
1950 *str = _("candidates are:");
1951 spaces = get_spaces (*str);
1952 }
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1954 *str = spaces ? spaces : *str;
1955 }
1956
1957 if (!more)
1958 {
1959 free (spaces);
1960 *str = NULL;
1961 }
1962}
1963
1964/* Print the list of candidate FNS in an error message. FNS can also
1965 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1966
1967void
1968print_candidates (tree fns)
1969{
1970 const char *str = NULL;
1971 print_candidates_1 (fns, false, &str);
1972 gcc_assert (str == NULL);
1973}
1974
1975/* Get a (possibly) constrained template declaration for the
1976 purpose of ordering candidates. */
1977static tree
1978get_template_for_ordering (tree list)
1979{
1980 gcc_assert (TREE_CODE (list) == TREE_LIST);
1981 tree f = TREE_VALUE (list);
1982 if (tree ti = DECL_TEMPLATE_INFO (f))
1983 return TI_TEMPLATE (ti);
1984 return f;
1985}
1986
1987/* Among candidates having the same signature, return the
1988 most constrained or NULL_TREE if there is no best candidate.
1989 If the signatures of candidates vary (e.g., template
1990 specialization vs. member function), then there can be no
1991 most constrained.
1992
1993 Note that we don't compare constraints on the functions
1994 themselves, but rather those of their templates. */
1995static tree
1996most_constrained_function (tree candidates)
1997{
1998 // Try to find the best candidate in a first pass.
1999 tree champ = candidates;
2000 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2001 {
2002 int winner = more_constrained (get_template_for_ordering (champ),
2003 get_template_for_ordering (c));
2004 if (winner == -1)
2005 champ = c; // The candidate is more constrained
2006 else if (winner == 0)
2007 return NULL_TREE; // Neither is more constrained
2008 }
2009
2010 // Verify that the champ is better than previous candidates.
2011 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2012 if (!more_constrained (get_template_for_ordering (champ),
2013 get_template_for_ordering (c)))
2014 return NULL_TREE;
2015 }
2016
2017 return champ;
2018}
2019
2020
2021/* Returns the template (one of the functions given by TEMPLATE_ID)
2022 which can be specialized to match the indicated DECL with the
2023 explicit template args given in TEMPLATE_ID. The DECL may be
2024 NULL_TREE if none is available. In that case, the functions in
2025 TEMPLATE_ID are non-members.
2026
2027 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2028 specialization of a member template.
2029
2030 The TEMPLATE_COUNT is the number of references to qualifying
2031 template classes that appeared in the name of the function. See
2032 check_explicit_specialization for a more accurate description.
2033
2034 TSK indicates what kind of template declaration (if any) is being
2035 declared. TSK_TEMPLATE indicates that the declaration given by
2036 DECL, though a FUNCTION_DECL, has template parameters, and is
2037 therefore a template function.
2038
2039 The template args (those explicitly specified and those deduced)
2040 are output in a newly created vector *TARGS_OUT.
2041
2042 If it is impossible to determine the result, an error message is
2043 issued. The error_mark_node is returned to indicate failure. */
2044
2045static tree
2046determine_specialization (tree template_id,
2047 tree decl,
2048 tree* targs_out,
2049 int need_member_template,
2050 int template_count,
2051 tmpl_spec_kind tsk)
2052{
2053 tree fns;
2054 tree targs;
2055 tree explicit_targs;
2056 tree candidates = NULL_TREE;
2057
2058 /* A TREE_LIST of templates of which DECL may be a specialization.
2059 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2060 corresponding TREE_PURPOSE is the set of template arguments that,
2061 when used to instantiate the template, would produce a function
2062 with the signature of DECL. */
2063 tree templates = NULL_TREE;
2064 int header_count;
2065 cp_binding_level *b;
2066
2067 *targs_out = NULL_TREE;
2068
2069 if (template_id == error_mark_node || decl == error_mark_node)
2070 return error_mark_node;
2071
2072 /* We shouldn't be specializing a member template of an
2073 unspecialized class template; we already gave an error in
2074 check_specialization_scope, now avoid crashing. */
2075 if (!VAR_P (decl)
2076 && template_count && DECL_CLASS_SCOPE_P (decl)
2077 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2078 {
2079 gcc_assert (errorcount);
2080 return error_mark_node;
2081 }
2082
2083 fns = TREE_OPERAND (template_id, 0);
2084 explicit_targs = TREE_OPERAND (template_id, 1);
2085
2086 if (fns == error_mark_node)
2087 return error_mark_node;
2088
2089 /* Check for baselinks. */
2090 if (BASELINK_P (fns))
2091 fns = BASELINK_FUNCTIONS (fns);
2092
2093 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2094 {
2095 error ("%qD is not a function template", fns);
2096 return error_mark_node;
2097 }
2098 else if (VAR_P (decl) && !variable_template_p (fns))
2099 {
2100 error ("%qD is not a variable template", fns);
2101 return error_mark_node;
2102 }
2103
2104 /* Count the number of template headers specified for this
2105 specialization. */
2106 header_count = 0;
2107 for (b = current_binding_level;
2108 b->kind == sk_template_parms;
2109 b = b->level_chain)
2110 ++header_count;
2111
2112 tree orig_fns = fns;
2113
2114 if (variable_template_p (fns))
2115 {
2116 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2117 targs = coerce_template_parms (parms, explicit_targs, fns,
2118 tf_warning_or_error,
2119 /*req_all*/true, /*use_defarg*/true);
2120 if (targs != error_mark_node)
2121 templates = tree_cons (targs, fns, templates);
2122 }
2123 else for (; fns; fns = OVL_NEXT (fns))
2124 {
2125 tree fn = OVL_CURRENT (fns);
2126
2127 if (TREE_CODE (fn) == TEMPLATE_DECL)
2128 {
2129 tree decl_arg_types;
2130 tree fn_arg_types;
2131 tree insttype;
2132
2133 /* In case of explicit specialization, we need to check if
2134 the number of template headers appearing in the specialization
2135 is correct. This is usually done in check_explicit_specialization,
2136 but the check done there cannot be exhaustive when specializing
2137 member functions. Consider the following code:
2138
2139 template <> void A<int>::f(int);
2140 template <> template <> void A<int>::f(int);
2141
2142 Assuming that A<int> is not itself an explicit specialization
2143 already, the first line specializes "f" which is a non-template
2144 member function, whilst the second line specializes "f" which
2145 is a template member function. So both lines are syntactically
2146 correct, and check_explicit_specialization does not reject
2147 them.
2148
2149 Here, we can do better, as we are matching the specialization
2150 against the declarations. We count the number of template
2151 headers, and we check if they match TEMPLATE_COUNT + 1
2152 (TEMPLATE_COUNT is the number of qualifying template classes,
2153 plus there must be another header for the member template
2154 itself).
2155
2156 Notice that if header_count is zero, this is not a
2157 specialization but rather a template instantiation, so there
2158 is no check we can perform here. */
2159 if (header_count && header_count != template_count + 1)
2160 continue;
2161
2162 /* Check that the number of template arguments at the
2163 innermost level for DECL is the same as for FN. */
2164 if (current_binding_level->kind == sk_template_parms
2165 && !current_binding_level->explicit_spec_p
2166 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2167 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2168 (current_template_parms))))
2169 continue;
2170
2171 /* DECL might be a specialization of FN. */
2172 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2173 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2174
2175 /* For a non-static member function, we need to make sure
2176 that the const qualification is the same. Since
2177 get_bindings does not try to merge the "this" parameter,
2178 we must do the comparison explicitly. */
2179 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2180 {
2181 if (!same_type_p (TREE_VALUE (fn_arg_types),
2182 TREE_VALUE (decl_arg_types)))
2183 continue;
2184
2185 /* And the ref-qualification. */
2186 if (type_memfn_rqual (TREE_TYPE (decl))
2187 != type_memfn_rqual (TREE_TYPE (fn)))
2188 continue;
2189 }
2190
2191 /* Skip the "this" parameter and, for constructors of
2192 classes with virtual bases, the VTT parameter. A
2193 full specialization of a constructor will have a VTT
2194 parameter, but a template never will. */
2195 decl_arg_types
2196 = skip_artificial_parms_for (decl, decl_arg_types);
2197 fn_arg_types
2198 = skip_artificial_parms_for (fn, fn_arg_types);
2199
2200 /* Function templates cannot be specializations; there are
2201 no partial specializations of functions. Therefore, if
2202 the type of DECL does not match FN, there is no
2203 match.
2204
2205 Note that it should never be the case that we have both
2206 candidates added here, and for regular member functions
2207 below. */
2208 if (tsk == tsk_template)
2209 {
2210 if (compparms (fn_arg_types, decl_arg_types))
2211 candidates = tree_cons (NULL_TREE, fn, candidates);
2212 continue;
2213 }
2214
2215 /* See whether this function might be a specialization of this
2216 template. Suppress access control because we might be trying
2217 to make this specialization a friend, and we have already done
2218 access control for the declaration of the specialization. */
2219 push_deferring_access_checks (dk_no_check);
2220 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2221 pop_deferring_access_checks ();
2222
2223 if (!targs)
2224 /* We cannot deduce template arguments that when used to
2225 specialize TMPL will produce DECL. */
2226 continue;
2227
2228 /* Remove, from the set of candidates, all those functions
2229 whose constraints are not satisfied. */
2230 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2231 continue;
2232
2233 // Then, try to form the new function type.
2234 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2235 if (insttype == error_mark_node)
2236 continue;
2237 fn_arg_types
2238 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2239 if (!compparms (fn_arg_types, decl_arg_types))
2240 continue;
2241
2242 /* Save this template, and the arguments deduced. */
2243 templates = tree_cons (targs, fn, templates);
2244 }
2245 else if (need_member_template)
2246 /* FN is an ordinary member function, and we need a
2247 specialization of a member template. */
2248 ;
2249 else if (TREE_CODE (fn) != FUNCTION_DECL)
2250 /* We can get IDENTIFIER_NODEs here in certain erroneous
2251 cases. */
2252 ;
2253 else if (!DECL_FUNCTION_MEMBER_P (fn))
2254 /* This is just an ordinary non-member function. Nothing can
2255 be a specialization of that. */
2256 ;
2257 else if (DECL_ARTIFICIAL (fn))
2258 /* Cannot specialize functions that are created implicitly. */
2259 ;
2260 else
2261 {
2262 tree decl_arg_types;
2263
2264 /* This is an ordinary member function. However, since
2265 we're here, we can assume its enclosing class is a
2266 template class. For example,
2267
2268 template <typename T> struct S { void f(); };
2269 template <> void S<int>::f() {}
2270
2271 Here, S<int>::f is a non-template, but S<int> is a
2272 template class. If FN has the same type as DECL, we
2273 might be in business. */
2274
2275 if (!DECL_TEMPLATE_INFO (fn))
2276 /* Its enclosing class is an explicit specialization
2277 of a template class. This is not a candidate. */
2278 continue;
2279
2280 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2281 TREE_TYPE (TREE_TYPE (fn))))
2282 /* The return types differ. */
2283 continue;
2284
2285 /* Adjust the type of DECL in case FN is a static member. */
2286 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2287 if (DECL_STATIC_FUNCTION_P (fn)
2288 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2289 decl_arg_types = TREE_CHAIN (decl_arg_types);
2290
2291 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2292 decl_arg_types))
2293 continue;
2294
2295 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2296 && (type_memfn_rqual (TREE_TYPE (decl))
2297 != type_memfn_rqual (TREE_TYPE (fn))))
2298 continue;
2299
2300 // If the deduced arguments do not satisfy the constraints,
2301 // this is not a candidate.
2302 if (flag_concepts && !constraints_satisfied_p (fn))
2303 continue;
2304
2305 // Add the candidate.
2306 candidates = tree_cons (NULL_TREE, fn, candidates);
2307 }
2308 }
2309
2310 if (templates && TREE_CHAIN (templates))
2311 {
2312 /* We have:
2313
2314 [temp.expl.spec]
2315
2316 It is possible for a specialization with a given function
2317 signature to be instantiated from more than one function
2318 template. In such cases, explicit specification of the
2319 template arguments must be used to uniquely identify the
2320 function template specialization being specialized.
2321
2322 Note that here, there's no suggestion that we're supposed to
2323 determine which of the candidate templates is most
2324 specialized. However, we, also have:
2325
2326 [temp.func.order]
2327
2328 Partial ordering of overloaded function template
2329 declarations is used in the following contexts to select
2330 the function template to which a function template
2331 specialization refers:
2332
2333 -- when an explicit specialization refers to a function
2334 template.
2335
2336 So, we do use the partial ordering rules, at least for now.
2337 This extension can only serve to make invalid programs valid,
2338 so it's safe. And, there is strong anecdotal evidence that
2339 the committee intended the partial ordering rules to apply;
2340 the EDG front end has that behavior, and John Spicer claims
2341 that the committee simply forgot to delete the wording in
2342 [temp.expl.spec]. */
2343 tree tmpl = most_specialized_instantiation (templates);
2344 if (tmpl != error_mark_node)
2345 {
2346 templates = tmpl;
2347 TREE_CHAIN (templates) = NULL_TREE;
2348 }
2349 }
2350
2351 // Concepts allows multiple declarations of member functions
2352 // with the same signature. Like above, we need to rely on
2353 // on the partial ordering of those candidates to determine which
2354 // is the best.
2355 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2356 {
2357 if (tree cand = most_constrained_function (candidates))
2358 {
2359 candidates = cand;
2360 TREE_CHAIN (cand) = NULL_TREE;
2361 }
2362 }
2363
2364 if (templates == NULL_TREE && candidates == NULL_TREE)
2365 {
2366 error ("template-id %qD for %q+D does not match any template "
2367 "declaration", template_id, decl);
2368 if (header_count && header_count != template_count + 1)
2369 inform (input_location, "saw %d %<template<>%>, need %d for "
2370 "specializing a member function template",
2371 header_count, template_count + 1);
2372 else
2373 print_candidates (orig_fns);
2374 return error_mark_node;
2375 }
2376 else if ((templates && TREE_CHAIN (templates))
2377 || (candidates && TREE_CHAIN (candidates))
2378 || (templates && candidates))
2379 {
2380 error ("ambiguous template specialization %qD for %q+D",
2381 template_id, decl);
2382 candidates = chainon (candidates, templates);
2383 print_candidates (candidates);
2384 return error_mark_node;
2385 }
2386
2387 /* We have one, and exactly one, match. */
2388 if (candidates)
2389 {
2390 tree fn = TREE_VALUE (candidates);
2391 *targs_out = copy_node (DECL_TI_ARGS (fn));
2392
2393 // Propagate the candidate's constraints to the declaration.
2394 set_constraints (decl, get_constraints (fn));
2395
2396 /* DECL is a re-declaration or partial instantiation of a template
2397 function. */
2398 if (TREE_CODE (fn) == TEMPLATE_DECL)
2399 return fn;
2400 /* It was a specialization of an ordinary member function in a
2401 template class. */
2402 return DECL_TI_TEMPLATE (fn);
2403 }
2404
2405 /* It was a specialization of a template. */
2406 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2407 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2408 {
2409 *targs_out = copy_node (targs);
2410 SET_TMPL_ARGS_LEVEL (*targs_out,
2411 TMPL_ARGS_DEPTH (*targs_out),
2412 TREE_PURPOSE (templates));
2413 }
2414 else
2415 *targs_out = TREE_PURPOSE (templates);
2416 return TREE_VALUE (templates);
2417}
2418
2419/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2420 but with the default argument values filled in from those in the
2421 TMPL_TYPES. */
2422
2423static tree
2424copy_default_args_to_explicit_spec_1 (tree spec_types,
2425 tree tmpl_types)
2426{
2427 tree new_spec_types;
2428
2429 if (!spec_types)
2430 return NULL_TREE;
2431
2432 if (spec_types == void_list_node)
2433 return void_list_node;
2434
2435 /* Substitute into the rest of the list. */
2436 new_spec_types =
2437 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2438 TREE_CHAIN (tmpl_types));
2439
2440 /* Add the default argument for this parameter. */
2441 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2442 TREE_VALUE (spec_types),
2443 new_spec_types);
2444}
2445
2446/* DECL is an explicit specialization. Replicate default arguments
2447 from the template it specializes. (That way, code like:
2448
2449 template <class T> void f(T = 3);
2450 template <> void f(double);
2451 void g () { f (); }
2452
2453 works, as required.) An alternative approach would be to look up
2454 the correct default arguments at the call-site, but this approach
2455 is consistent with how implicit instantiations are handled. */
2456
2457static void
2458copy_default_args_to_explicit_spec (tree decl)
2459{
2460 tree tmpl;
2461 tree spec_types;
2462 tree tmpl_types;
2463 tree new_spec_types;
2464 tree old_type;
2465 tree new_type;
2466 tree t;
2467 tree object_type = NULL_TREE;
2468 tree in_charge = NULL_TREE;
2469 tree vtt = NULL_TREE;
2470
2471 /* See if there's anything we need to do. */
2472 tmpl = DECL_TI_TEMPLATE (decl);
2473 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2474 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2475 if (TREE_PURPOSE (t))
2476 break;
2477 if (!t)
2478 return;
2479
2480 old_type = TREE_TYPE (decl);
2481 spec_types = TYPE_ARG_TYPES (old_type);
2482
2483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2484 {
2485 /* Remove the this pointer, but remember the object's type for
2486 CV quals. */
2487 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2488 spec_types = TREE_CHAIN (spec_types);
2489 tmpl_types = TREE_CHAIN (tmpl_types);
2490
2491 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2492 {
2493 /* DECL may contain more parameters than TMPL due to the extra
2494 in-charge parameter in constructors and destructors. */
2495 in_charge = spec_types;
2496 spec_types = TREE_CHAIN (spec_types);
2497 }
2498 if (DECL_HAS_VTT_PARM_P (decl))
2499 {
2500 vtt = spec_types;
2501 spec_types = TREE_CHAIN (spec_types);
2502 }
2503 }
2504
2505 /* Compute the merged default arguments. */
2506 new_spec_types =
2507 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2508
2509 /* Compute the new FUNCTION_TYPE. */
2510 if (object_type)
2511 {
2512 if (vtt)
2513 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2514 TREE_VALUE (vtt),
2515 new_spec_types);
2516
2517 if (in_charge)
2518 /* Put the in-charge parameter back. */
2519 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2520 TREE_VALUE (in_charge),
2521 new_spec_types);
2522
2523 new_type = build_method_type_directly (object_type,
2524 TREE_TYPE (old_type),
2525 new_spec_types);
2526 }
2527 else
2528 new_type = build_function_type (TREE_TYPE (old_type),
2529 new_spec_types);
2530 new_type = cp_build_type_attribute_variant (new_type,
2531 TYPE_ATTRIBUTES (old_type));
2532 new_type = build_exception_variant (new_type,
2533 TYPE_RAISES_EXCEPTIONS (old_type));
2534
2535 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2536 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2537
2538 TREE_TYPE (decl) = new_type;
2539}
2540
2541/* Return the number of template headers we expect to see for a definition
2542 or specialization of CTYPE or one of its non-template members. */
2543
2544int
2545num_template_headers_for_class (tree ctype)
2546{
2547 int num_templates = 0;
2548
2549 while (ctype && CLASS_TYPE_P (ctype))
2550 {
2551 /* You're supposed to have one `template <...>' for every
2552 template class, but you don't need one for a full
2553 specialization. For example:
2554
2555 template <class T> struct S{};
2556 template <> struct S<int> { void f(); };
2557 void S<int>::f () {}
2558
2559 is correct; there shouldn't be a `template <>' for the
2560 definition of `S<int>::f'. */
2561 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2562 /* If CTYPE does not have template information of any
2563 kind, then it is not a template, nor is it nested
2564 within a template. */
2565 break;
2566 if (explicit_class_specialization_p (ctype))
2567 break;
2568 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2569 ++num_templates;
2570
2571 ctype = TYPE_CONTEXT (ctype);
2572 }
2573
2574 return num_templates;
2575}
2576
2577/* Do a simple sanity check on the template headers that precede the
2578 variable declaration DECL. */
2579
2580void
2581check_template_variable (tree decl)
2582{
2583 tree ctx = CP_DECL_CONTEXT (decl);
2584 int wanted = num_template_headers_for_class (ctx);
2585 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2586 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2587 {
2588 if (cxx_dialect < cxx14)
2589 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2590 "variable templates only available with "
2591 "-std=c++14 or -std=gnu++14");
2592
2593 // Namespace-scope variable templates should have a template header.
2594 ++wanted;
2595 }
2596 if (template_header_count > wanted)
2597 {
2598 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2599 "too many template headers for %D (should be %d)",
2600 decl, wanted);
2601 if (warned && CLASS_TYPE_P (ctx)
2602 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2603 inform (DECL_SOURCE_LOCATION (decl),
2604 "members of an explicitly specialized class are defined "
2605 "without a template header");
2606 }
2607}
2608
2609/* An explicit specialization whose declarator-id or class-head-name is not
2610 qualified shall be declared in the nearest enclosing namespace of the
2611 template, or, if the namespace is inline (7.3.1), any namespace from its
2612 enclosing namespace set.
2613
2614 If the name declared in the explicit instantiation is an unqualified name,
2615 the explicit instantiation shall appear in the namespace where its template
2616 is declared or, if that namespace is inline (7.3.1), any namespace from its
2617 enclosing namespace set. */
2618
2619void
2620check_unqualified_spec_or_inst (tree t, location_t loc)
2621{
2622 tree tmpl = most_general_template (t);
2623 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2624 && !is_associated_namespace (current_namespace,
2625 CP_DECL_CONTEXT (tmpl)))
2626 {
2627 if (processing_specialization)
2628 permerror (loc, "explicit specialization of %qD outside its "
2629 "namespace must use a nested-name-specifier", tmpl);
2630 else if (processing_explicit_instantiation
2631 && cxx_dialect >= cxx11)
2632 /* This was allowed in C++98, so only pedwarn. */
2633 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2634 "outside its namespace must use a nested-name-"
2635 "specifier", tmpl);
2636 }
2637}
2638
2639/* Check to see if the function just declared, as indicated in
2640 DECLARATOR, and in DECL, is a specialization of a function
2641 template. We may also discover that the declaration is an explicit
2642 instantiation at this point.
2643
2644 Returns DECL, or an equivalent declaration that should be used
2645 instead if all goes well. Issues an error message if something is
2646 amiss. Returns error_mark_node if the error is not easily
2647 recoverable.
2648
2649 FLAGS is a bitmask consisting of the following flags:
2650
2651 2: The function has a definition.
2652 4: The function is a friend.
2653
2654 The TEMPLATE_COUNT is the number of references to qualifying
2655 template classes that appeared in the name of the function. For
2656 example, in
2657
2658 template <class T> struct S { void f(); };
2659 void S<int>::f();
2660
2661 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2662 classes are not counted in the TEMPLATE_COUNT, so that in
2663
2664 template <class T> struct S {};
2665 template <> struct S<int> { void f(); }
2666 template <> void S<int>::f();
2667
2668 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2669 invalid; there should be no template <>.)
2670
2671 If the function is a specialization, it is marked as such via
2672 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2673 is set up correctly, and it is added to the list of specializations
2674 for that template. */
2675
2676tree
2677check_explicit_specialization (tree declarator,
2678 tree decl,
2679 int template_count,
2680 int flags)
2681{
2682 int have_def = flags & 2;
2683 int is_friend = flags & 4;
2684 bool is_concept = flags & 8;
2685 int specialization = 0;
2686 int explicit_instantiation = 0;
2687 int member_specialization = 0;
2688 tree ctype = DECL_CLASS_CONTEXT (decl);
2689 tree dname = DECL_NAME (decl);
2690 tmpl_spec_kind tsk;
2691
2692 if (is_friend)
2693 {
2694 if (!processing_specialization)
2695 tsk = tsk_none;
2696 else
2697 tsk = tsk_excessive_parms;
2698 }
2699 else
2700 tsk = current_tmpl_spec_kind (template_count);
2701
2702 switch (tsk)
2703 {
2704 case tsk_none:
2705 if (processing_specialization && !VAR_P (decl))
2706 {
2707 specialization = 1;
2708 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2709 }
2710 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2711 {
2712 if (is_friend)
2713 /* This could be something like:
2714
2715 template <class T> void f(T);
2716 class S { friend void f<>(int); } */
2717 specialization = 1;
2718 else
2719 {
2720 /* This case handles bogus declarations like template <>
2721 template <class T> void f<int>(); */
2722
2723 error ("template-id %qD in declaration of primary template",
2724 declarator);
2725 return decl;
2726 }
2727 }
2728 break;
2729
2730 case tsk_invalid_member_spec:
2731 /* The error has already been reported in
2732 check_specialization_scope. */
2733 return error_mark_node;
2734
2735 case tsk_invalid_expl_inst:
2736 error ("template parameter list used in explicit instantiation");
2737
2738 /* Fall through. */
2739
2740 case tsk_expl_inst:
2741 if (have_def)
2742 error ("definition provided for explicit instantiation");
2743
2744 explicit_instantiation = 1;
2745 break;
2746
2747 case tsk_excessive_parms:
2748 case tsk_insufficient_parms:
2749 if (tsk == tsk_excessive_parms)
2750 error ("too many template parameter lists in declaration of %qD",
2751 decl);
2752 else if (template_header_count)
2753 error("too few template parameter lists in declaration of %qD", decl);
2754 else
2755 error("explicit specialization of %qD must be introduced by "
2756 "%<template <>%>", decl);
2757
2758 /* Fall through. */
2759 case tsk_expl_spec:
2760 if (is_concept)
2761 error ("explicit specialization declared %<concept%>");
2762
2763 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2764 /* In cases like template<> constexpr bool v = true;
2765 We'll give an error in check_template_variable. */
2766 break;
2767
2768 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2769 if (ctype)
2770 member_specialization = 1;
2771 else
2772 specialization = 1;
2773 break;
2774
2775 case tsk_template:
2776 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2777 {
2778 /* This case handles bogus declarations like template <>
2779 template <class T> void f<int>(); */
2780
2781 if (!uses_template_parms (declarator))
2782 error ("template-id %qD in declaration of primary template",
2783 declarator);
2784 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2785 {
2786 /* Partial specialization of variable template. */
2787 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2788 specialization = 1;
2789 goto ok;
2790 }
2791 else if (cxx_dialect < cxx14)
2792 error ("non-type partial specialization %qD "
2793 "is not allowed", declarator);
2794 else
2795 error ("non-class, non-variable partial specialization %qD "
2796 "is not allowed", declarator);
2797 return decl;
2798 ok:;
2799 }
2800
2801 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2802 /* This is a specialization of a member template, without
2803 specialization the containing class. Something like:
2804
2805 template <class T> struct S {
2806 template <class U> void f (U);
2807 };
2808 template <> template <class U> void S<int>::f(U) {}
2809
2810 That's a specialization -- but of the entire template. */
2811 specialization = 1;
2812 break;
2813
2814 default:
2815 gcc_unreachable ();
2816 }
2817
2818 if ((specialization || member_specialization)
2819 /* This doesn't apply to variable templates. */
2820 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2821 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2822 {
2823 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2824 for (; t; t = TREE_CHAIN (t))
2825 if (TREE_PURPOSE (t))
2826 {
2827 permerror (input_location,
2828 "default argument specified in explicit specialization");
2829 break;
2830 }
2831 }
2832
2833 if (specialization || member_specialization || explicit_instantiation)
2834 {
2835 tree tmpl = NULL_TREE;
2836 tree targs = NULL_TREE;
2837 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2838
2839 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2840 if (!was_template_id)
2841 {
2842 tree fns;
2843
2844 gcc_assert (identifier_p (declarator));
2845 if (ctype)
2846 fns = dname;
2847 else
2848 {
2849 /* If there is no class context, the explicit instantiation
2850 must be at namespace scope. */
2851 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2852
2853 /* Find the namespace binding, using the declaration
2854 context. */
2855 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2856 false, true);
2857 if (fns == error_mark_node)
2858 /* If lookup fails, look for a friend declaration so we can
2859 give a better diagnostic. */
2860 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2861 /*type*/false, /*complain*/true,
2862 /*hidden*/true);
2863
2864 if (fns == error_mark_node || !is_overloaded_fn (fns))
2865 {
2866 error ("%qD is not a template function", dname);
2867 fns = error_mark_node;
2868 }
2869 }
2870
2871 declarator = lookup_template_function (fns, NULL_TREE);
2872 }
2873
2874 if (declarator == error_mark_node)
2875 return error_mark_node;
2876
2877 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2878 {
2879 if (!explicit_instantiation)
2880 /* A specialization in class scope. This is invalid,
2881 but the error will already have been flagged by
2882 check_specialization_scope. */
2883 return error_mark_node;
2884 else
2885 {
2886 /* It's not valid to write an explicit instantiation in
2887 class scope, e.g.:
2888
2889 class C { template void f(); }
2890
2891 This case is caught by the parser. However, on
2892 something like:
2893
2894 template class C { void f(); };
2895
2896 (which is invalid) we can get here. The error will be
2897 issued later. */
2898 ;
2899 }
2900
2901 return decl;
2902 }
2903 else if (ctype != NULL_TREE
2904 && (identifier_p (TREE_OPERAND (declarator, 0))))
2905 {
2906 // We'll match variable templates in start_decl.
2907 if (VAR_P (decl))
2908 return decl;
2909
2910 /* Find the list of functions in ctype that have the same
2911 name as the declared function. */
2912 tree name = TREE_OPERAND (declarator, 0);
2913 tree fns = NULL_TREE;
2914 int idx;
2915
2916 if (constructor_name_p (name, ctype))
2917 {
2918 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2919
2920 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2921 : !CLASSTYPE_DESTRUCTORS (ctype))
2922 {
2923 /* From [temp.expl.spec]:
2924
2925 If such an explicit specialization for the member
2926 of a class template names an implicitly-declared
2927 special member function (clause _special_), the
2928 program is ill-formed.
2929
2930 Similar language is found in [temp.explicit]. */
2931 error ("specialization of implicitly-declared special member function");
2932 return error_mark_node;
2933 }
2934
2935 name = is_constructor ? ctor_identifier : dtor_identifier;
2936 }
2937
2938 if (!DECL_CONV_FN_P (decl))
2939 {
2940 idx = lookup_fnfields_1 (ctype, name);
2941 if (idx >= 0)
2942 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2943 }
2944 else
2945 {
2946 vec<tree, va_gc> *methods;
2947 tree ovl;
2948
2949 /* For a type-conversion operator, we cannot do a
2950 name-based lookup. We might be looking for `operator
2951 int' which will be a specialization of `operator T'.
2952 So, we find *all* the conversion operators, and then
2953 select from them. */
2954 fns = NULL_TREE;
2955
2956 methods = CLASSTYPE_METHOD_VEC (ctype);
2957 if (methods)
2958 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2959 methods->iterate (idx, &ovl);
2960 ++idx)
2961 {
2962 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2963 /* There are no more conversion functions. */
2964 break;
2965
2966 /* Glue all these conversion functions together
2967 with those we already have. */
2968 for (; ovl; ovl = OVL_NEXT (ovl))
2969 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2970 }
2971 }
2972
2973 if (fns == NULL_TREE)
2974 {
2975 error ("no member function %qD declared in %qT", name, ctype);
2976 return error_mark_node;
2977 }
2978 else
2979 TREE_OPERAND (declarator, 0) = fns;
2980 }
2981
2982 /* Figure out what exactly is being specialized at this point.
2983 Note that for an explicit instantiation, even one for a
2984 member function, we cannot tell a priori whether the
2985 instantiation is for a member template, or just a member
2986 function of a template class. Even if a member template is
2987 being instantiated, the member template arguments may be
2988 elided if they can be deduced from the rest of the
2989 declaration. */
2990 tmpl = determine_specialization (declarator, decl,
2991 &targs,
2992 member_specialization,
2993 template_count,
2994 tsk);
2995
2996 if (!tmpl || tmpl == error_mark_node)
2997 /* We couldn't figure out what this declaration was
2998 specializing. */
2999 return error_mark_node;
3000 else
3001 {
3002 if (TREE_CODE (decl) == FUNCTION_DECL
3003 && DECL_HIDDEN_FRIEND_P (tmpl))
3004 {
3005 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3006 "friend declaration %qD is not visible to "
3007 "explicit specialization", tmpl))
3008 inform (DECL_SOURCE_LOCATION (tmpl),
3009 "friend declaration here");
3010 }
3011 else if (!ctype && !is_friend
3012 && CP_DECL_CONTEXT (decl) == current_namespace)
3013 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3014
3015 tree gen_tmpl = most_general_template (tmpl);
3016
3017 if (explicit_instantiation)
3018 {
3019 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3020 is done by do_decl_instantiation later. */
3021
3022 int arg_depth = TMPL_ARGS_DEPTH (targs);
3023 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3024
3025 if (arg_depth > parm_depth)
3026 {
3027 /* If TMPL is not the most general template (for
3028 example, if TMPL is a friend template that is
3029 injected into namespace scope), then there will
3030 be too many levels of TARGS. Remove some of them
3031 here. */
3032 int i;
3033 tree new_targs;
3034
3035 new_targs = make_tree_vec (parm_depth);
3036 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3037 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3038 = TREE_VEC_ELT (targs, i);
3039 targs = new_targs;
3040 }
3041
3042 return instantiate_template (tmpl, targs, tf_error);
3043 }
3044
3045 /* If we thought that the DECL was a member function, but it
3046 turns out to be specializing a static member function,
3047 make DECL a static member function as well. */
3048 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3049 && DECL_STATIC_FUNCTION_P (tmpl)
3050 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3051 revert_static_member_fn (decl);
3052
3053 /* If this is a specialization of a member template of a
3054 template class, we want to return the TEMPLATE_DECL, not
3055 the specialization of it. */
3056 if (tsk == tsk_template && !was_template_id)
3057 {
3058 tree result = DECL_TEMPLATE_RESULT (tmpl);
3059 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3060 DECL_INITIAL (result) = NULL_TREE;
3061 if (have_def)
3062 {
3063 tree parm;
3064 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3065 DECL_SOURCE_LOCATION (result)
3066 = DECL_SOURCE_LOCATION (decl);
3067 /* We want to use the argument list specified in the
3068 definition, not in the original declaration. */
3069 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3070 for (parm = DECL_ARGUMENTS (result); parm;
3071 parm = DECL_CHAIN (parm))
3072 DECL_CONTEXT (parm) = result;
3073 }
3074 return register_specialization (tmpl, gen_tmpl, targs,
3075 is_friend, 0);
3076 }
3077
3078 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3079 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3080
3081 if (was_template_id)
3082 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3083
3084 /* Inherit default function arguments from the template
3085 DECL is specializing. */
3086 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3087 copy_default_args_to_explicit_spec (decl);
3088
3089 /* This specialization has the same protection as the
3090 template it specializes. */
3091 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3092 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3093
3094 /* 7.1.1-1 [dcl.stc]
3095
3096 A storage-class-specifier shall not be specified in an
3097 explicit specialization...
3098
3099 The parser rejects these, so unless action is taken here,
3100 explicit function specializations will always appear with
3101 global linkage.
3102
3103 The action recommended by the C++ CWG in response to C++
3104 defect report 605 is to make the storage class and linkage
3105 of the explicit specialization match the templated function:
3106
3107 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3108 */
3109 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3110 {
3111 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3112 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3113
3114 /* A concept cannot be specialized. */
3115 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3116 {
3117 error ("explicit specialization of function concept %qD",
3118 gen_tmpl);
3119 return error_mark_node;
3120 }
3121
3122 /* This specialization has the same linkage and visibility as
3123 the function template it specializes. */
3124 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3125 if (! TREE_PUBLIC (decl))
3126 {
3127 DECL_INTERFACE_KNOWN (decl) = 1;
3128 DECL_NOT_REALLY_EXTERN (decl) = 1;
3129 }
3130 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3131 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3132 {
3133 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3134 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3135 }
3136 }
3137
3138 /* If DECL is a friend declaration, declared using an
3139 unqualified name, the namespace associated with DECL may
3140 have been set incorrectly. For example, in:
3141
3142 template <typename T> void f(T);
3143 namespace N {
3144 struct S { friend void f<int>(int); }
3145 }
3146
3147 we will have set the DECL_CONTEXT for the friend
3148 declaration to N, rather than to the global namespace. */
3149 if (DECL_NAMESPACE_SCOPE_P (decl))
3150 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3151
3152 if (is_friend && !have_def)
3153 /* This is not really a declaration of a specialization.
3154 It's just the name of an instantiation. But, it's not
3155 a request for an instantiation, either. */
3156 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3157 else if (TREE_CODE (decl) == FUNCTION_DECL)
3158 /* A specialization is not necessarily COMDAT. */
3159 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3160 && DECL_DECLARED_INLINE_P (decl));
3161 else if (VAR_P (decl))
3162 DECL_COMDAT (decl) = false;
3163
3164 /* If this is a full specialization, register it so that we can find
3165 it again. Partial specializations will be registered in
3166 process_partial_specialization. */
3167 if (!processing_template_decl)
3168 decl = register_specialization (decl, gen_tmpl, targs,
3169 is_friend, 0);
3170
3171 /* A 'structor should already have clones. */
3172 gcc_assert (decl == error_mark_node
3173 || variable_template_p (tmpl)
3174 || !(DECL_CONSTRUCTOR_P (decl)
3175 || DECL_DESTRUCTOR_P (decl))
3176 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3177 }
3178 }
3179
3180 return decl;
3181}
3182
3183/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3184 parameters. These are represented in the same format used for
3185 DECL_TEMPLATE_PARMS. */
3186
3187int
3188comp_template_parms (const_tree parms1, const_tree parms2)
3189{
3190 const_tree p1;
3191 const_tree p2;
3192
3193 if (parms1 == parms2)
3194 return 1;
3195
3196 for (p1 = parms1, p2 = parms2;
3197 p1 != NULL_TREE && p2 != NULL_TREE;
3198 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3199 {
3200 tree t1 = TREE_VALUE (p1);
3201 tree t2 = TREE_VALUE (p2);
3202 int i;
3203
3204 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3205 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3206
3207 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3208 return 0;
3209
3210 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3211 {
3212 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3213 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3214
3215 /* If either of the template parameters are invalid, assume
3216 they match for the sake of error recovery. */
3217 if (error_operand_p (parm1) || error_operand_p (parm2))
3218 return 1;
3219
3220 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3221 return 0;
3222
3223 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3224 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3225 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3226 continue;
3227 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3228 return 0;
3229 }
3230 }
3231
3232 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3233 /* One set of parameters has more parameters lists than the
3234 other. */
3235 return 0;
3236
3237 return 1;
3238}
3239
3240/* Determine whether PARM is a parameter pack. */
3241
3242bool
3243template_parameter_pack_p (const_tree parm)
3244{
3245 /* Determine if we have a non-type template parameter pack. */
3246 if (TREE_CODE (parm) == PARM_DECL)
3247 return (DECL_TEMPLATE_PARM_P (parm)
3248 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3249 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3250 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3251
3252 /* If this is a list of template parameters, we could get a
3253 TYPE_DECL or a TEMPLATE_DECL. */
3254 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3255 parm = TREE_TYPE (parm);
3256
3257 /* Otherwise it must be a type template parameter. */
3258 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3259 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3260 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3261}
3262
3263/* Determine if T is a function parameter pack. */
3264
3265bool
3266function_parameter_pack_p (const_tree t)
3267{
3268 if (t && TREE_CODE (t) == PARM_DECL)
3269 return DECL_PACK_P (t);
3270 return false;
3271}
3272
3273/* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3274 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3275
3276tree
3277get_function_template_decl (const_tree primary_func_tmpl_inst)
3278{
3279 if (! primary_func_tmpl_inst
3280 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3281 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3282 return NULL;
3283
3284 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3285}
3286
3287/* Return true iff the function parameter PARAM_DECL was expanded
3288 from the function parameter pack PACK. */
3289
3290bool
3291function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3292{
3293 if (DECL_ARTIFICIAL (param_decl)
3294 || !function_parameter_pack_p (pack))
3295 return false;
3296
3297 /* The parameter pack and its pack arguments have the same
3298 DECL_PARM_INDEX. */
3299 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3300}
3301
3302/* Determine whether ARGS describes a variadic template args list,
3303 i.e., one that is terminated by a template argument pack. */
3304
3305static bool
3306template_args_variadic_p (tree args)
3307{
3308 int nargs;
3309 tree last_parm;
3310
3311 if (args == NULL_TREE)
3312 return false;
3313
3314 args = INNERMOST_TEMPLATE_ARGS (args);
3315 nargs = TREE_VEC_LENGTH (args);
3316
3317 if (nargs == 0)
3318 return false;
3319
3320 last_parm = TREE_VEC_ELT (args, nargs - 1);
3321
3322 return ARGUMENT_PACK_P (last_parm);
3323}
3324
3325/* Generate a new name for the parameter pack name NAME (an
3326 IDENTIFIER_NODE) that incorporates its */
3327
3328static tree
3329make_ith_pack_parameter_name (tree name, int i)
3330{
3331 /* Munge the name to include the parameter index. */
3332#define NUMBUF_LEN 128
3333 char numbuf[NUMBUF_LEN];
3334 char* newname;
3335 int newname_len;
3336
3337 if (name == NULL_TREE)
3338 return name;
3339 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3340 newname_len = IDENTIFIER_LENGTH (name)
3341 + strlen (numbuf) + 2;
3342 newname = (char*)alloca (newname_len);
3343 snprintf (newname, newname_len,
3344 "%s#%i", IDENTIFIER_POINTER (name), i);
3345 return get_identifier (newname);
3346}
3347
3348/* Return true if T is a primary function, class or alias template
3349 instantiation. */
3350
3351bool
3352primary_template_instantiation_p (const_tree t)
3353{
3354 if (!t)
3355 return false;
3356
3357 if (TREE_CODE (t) == FUNCTION_DECL)
3358 return DECL_LANG_SPECIFIC (t)
3359 && DECL_TEMPLATE_INSTANTIATION (t)
3360 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3361 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3362 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3363 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3364 else if (alias_template_specialization_p (t))
3365 return true;
3366 return false;
3367}
3368
3369/* Return true if PARM is a template template parameter. */
3370
3371bool
3372template_template_parameter_p (const_tree parm)
3373{
3374 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3375}
3376
3377/* Return true iff PARM is a DECL representing a type template
3378 parameter. */
3379
3380bool
3381template_type_parameter_p (const_tree parm)
3382{
3383 return (parm
3384 && (TREE_CODE (parm) == TYPE_DECL
3385 || TREE_CODE (parm) == TEMPLATE_DECL)
3386 && DECL_TEMPLATE_PARM_P (parm));
3387}
3388
3389/* Return the template parameters of T if T is a
3390 primary template instantiation, NULL otherwise. */
3391
3392tree
3393get_primary_template_innermost_parameters (const_tree t)
3394{
3395 tree parms = NULL, template_info = NULL;
3396
3397 if ((template_info = get_template_info (t))
3398 && primary_template_instantiation_p (t))
3399 parms = INNERMOST_TEMPLATE_PARMS
3400 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3401
3402 return parms;
3403}
3404
3405/* Return the template parameters of the LEVELth level from the full list
3406 of template parameters PARMS. */
3407
3408tree
3409get_template_parms_at_level (tree parms, int level)
3410{
3411 tree p;
3412 if (!parms
3413 || TREE_CODE (parms) != TREE_LIST
3414 || level > TMPL_PARMS_DEPTH (parms))
3415 return NULL_TREE;
3416
3417 for (p = parms; p; p = TREE_CHAIN (p))
3418 if (TMPL_PARMS_DEPTH (p) == level)
3419 return p;
3420
3421 return NULL_TREE;
3422}
3423
3424/* Returns the template arguments of T if T is a template instantiation,
3425 NULL otherwise. */
3426
3427tree
3428get_template_innermost_arguments (const_tree t)
3429{
3430 tree args = NULL, template_info = NULL;
3431
3432 if ((template_info = get_template_info (t))
3433 && TI_ARGS (template_info))
3434 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3435
3436 return args;
3437}
3438
3439/* Return the argument pack elements of T if T is a template argument pack,
3440 NULL otherwise. */
3441
3442tree
3443get_template_argument_pack_elems (const_tree t)
3444{
3445 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3446 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3447 return NULL;
3448
3449 return ARGUMENT_PACK_ARGS (t);
3450}
3451
3452/* Structure used to track the progress of find_parameter_packs_r. */
3453struct find_parameter_pack_data
3454{
3455 /* TREE_LIST that will contain all of the parameter packs found by
3456 the traversal. */
3457 tree* parameter_packs;
3458
3459 /* Set of AST nodes that have been visited by the traversal. */
3460 hash_set<tree> *visited;
3461
3462 /* True iff we're making a type pack expansion. */
3463 bool type_pack_expansion_p;
3464};
3465
3466/* Identifies all of the argument packs that occur in a template
3467 argument and appends them to the TREE_LIST inside DATA, which is a
3468 find_parameter_pack_data structure. This is a subroutine of
3469 make_pack_expansion and uses_parameter_packs. */
3470static tree
3471find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3472{
3473 tree t = *tp;
3474 struct find_parameter_pack_data* ppd =
3475 (struct find_parameter_pack_data*)data;
3476 bool parameter_pack_p = false;
3477
3478 /* Handle type aliases/typedefs. */
3479 if (TYPE_ALIAS_P (t))
3480 {
3481 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3482 cp_walk_tree (&TI_ARGS (tinfo),
3483 &find_parameter_packs_r,
3484 ppd, ppd->visited);
3485 *walk_subtrees = 0;
3486 return NULL_TREE;
3487 }
3488
3489 /* Identify whether this is a parameter pack or not. */
3490 switch (TREE_CODE (t))
3491 {
3492 case TEMPLATE_PARM_INDEX:
3493 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3494 parameter_pack_p = true;
3495 break;
3496
3497 case TEMPLATE_TYPE_PARM:
3498 t = TYPE_MAIN_VARIANT (t);
3499 /* FALLTHRU */
3500 case TEMPLATE_TEMPLATE_PARM:
3501 /* If the placeholder appears in the decl-specifier-seq of a function
3502 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3503 is a pack expansion, the invented template parameter is a template
3504 parameter pack. */
3505 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3506 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3507 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3508 parameter_pack_p = true;
3509 break;
3510
3511 case FIELD_DECL:
3512 case PARM_DECL:
3513 if (DECL_PACK_P (t))
3514 {
3515 /* We don't want to walk into the type of a PARM_DECL,
3516 because we don't want to see the type parameter pack. */
3517 *walk_subtrees = 0;
3518 parameter_pack_p = true;
3519 }
3520 break;
3521
3522 /* Look through a lambda capture proxy to the field pack. */
3523 case VAR_DECL:
3524 if (DECL_HAS_VALUE_EXPR_P (t))
3525 {
3526 tree v = DECL_VALUE_EXPR (t);
3527 cp_walk_tree (&v,
3528 &find_parameter_packs_r,
3529 ppd, ppd->visited);
3530 *walk_subtrees = 0;
3531 }
3532 else if (variable_template_specialization_p (t))
3533 {
3534 cp_walk_tree (&DECL_TI_ARGS (t),
3535 find_parameter_packs_r,
3536 ppd, ppd->visited);
3537 *walk_subtrees = 0;
3538 }
3539 break;
3540
3541 case BASES:
3542 parameter_pack_p = true;
3543 break;
3544 default:
3545 /* Not a parameter pack. */
3546 break;
3547 }
3548
3549 if (parameter_pack_p)
3550 {
3551 /* Add this parameter pack to the list. */
3552 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3553 }
3554
3555 if (TYPE_P (t))
3556 cp_walk_tree (&TYPE_CONTEXT (t),
3557 &find_parameter_packs_r, ppd, ppd->visited);
3558
3559 /* This switch statement will return immediately if we don't find a
3560 parameter pack. */
3561 switch (TREE_CODE (t))
3562 {
3563 case TEMPLATE_PARM_INDEX:
3564 return NULL_TREE;
3565
3566 case BOUND_TEMPLATE_TEMPLATE_PARM:
3567 /* Check the template itself. */
3568 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3569 &find_parameter_packs_r, ppd, ppd->visited);
3570 /* Check the template arguments. */
3571 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3572 ppd->visited);
3573 *walk_subtrees = 0;
3574 return NULL_TREE;
3575
3576 case TEMPLATE_TYPE_PARM:
3577 case TEMPLATE_TEMPLATE_PARM:
3578 return NULL_TREE;
3579
3580 case PARM_DECL:
3581 return NULL_TREE;
3582
3583 case RECORD_TYPE:
3584 if (TYPE_PTRMEMFUNC_P (t))
3585 return NULL_TREE;
3586 /* Fall through. */
3587
3588 case UNION_TYPE:
3589 case ENUMERAL_TYPE:
3590 if (TYPE_TEMPLATE_INFO (t))
3591 cp_walk_tree (&TYPE_TI_ARGS (t),
3592 &find_parameter_packs_r, ppd, ppd->visited);
3593
3594 *walk_subtrees = 0;
3595 return NULL_TREE;
3596
3597 case TEMPLATE_DECL:
3598 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3599 return NULL_TREE;
3600 gcc_fallthrough();
3601
3602 case CONSTRUCTOR:
3603 cp_walk_tree (&TREE_TYPE (t),
3604 &find_parameter_packs_r, ppd, ppd->visited);
3605 return NULL_TREE;
3606
3607 case TYPENAME_TYPE:
3608 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3609 ppd, ppd->visited);
3610 *walk_subtrees = 0;
3611 return NULL_TREE;
3612
3613 case TYPE_PACK_EXPANSION:
3614 case EXPR_PACK_EXPANSION:
3615 *walk_subtrees = 0;
3616 return NULL_TREE;
3617
3618 case INTEGER_TYPE:
3619 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3620 ppd, ppd->visited);
3621 *walk_subtrees = 0;
3622 return NULL_TREE;
3623
3624 case IDENTIFIER_NODE:
3625 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3626 ppd->visited);
3627 *walk_subtrees = 0;
3628 return NULL_TREE;
3629
3630 case DECLTYPE_TYPE:
3631 {
3632 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3633 type_pack_expansion_p to false so that any placeholders
3634 within the expression don't get marked as parameter packs. */
3635 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3636 ppd->type_pack_expansion_p = false;
3637 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3638 ppd, ppd->visited);
3639 ppd->type_pack_expansion_p = type_pack_expansion_p;
3640 *walk_subtrees = 0;
3641 return NULL_TREE;
3642 }
3643
3644 default:
3645 return NULL_TREE;
3646 }
3647
3648 return NULL_TREE;
3649}
3650
3651/* Determines if the expression or type T uses any parameter packs. */
3652bool
3653uses_parameter_packs (tree t)
3654{
3655 tree parameter_packs = NULL_TREE;
3656 struct find_parameter_pack_data ppd;
3657 ppd.parameter_packs = &parameter_packs;
3658 ppd.visited = new hash_set<tree>;
3659 ppd.type_pack_expansion_p = false;
3660 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3661 delete ppd.visited;
3662 return parameter_packs != NULL_TREE;
3663}
3664
3665/* Turn ARG, which may be an expression, type, or a TREE_LIST
3666 representation a base-class initializer into a parameter pack
3667 expansion. If all goes well, the resulting node will be an
3668 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3669 respectively. */
3670tree
3671make_pack_expansion (tree arg)
3672{
3673 tree result;
3674 tree parameter_packs = NULL_TREE;
3675 bool for_types = false;
3676 struct find_parameter_pack_data ppd;
3677
3678 if (!arg || arg == error_mark_node)
3679 return arg;
3680
3681 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3682 {
3683 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3684 class initializer. In this case, the TREE_PURPOSE will be a
3685 _TYPE node (representing the base class expansion we're
3686 initializing) and the TREE_VALUE will be a TREE_LIST
3687 containing the initialization arguments.
3688
3689 The resulting expansion looks somewhat different from most
3690 expansions. Rather than returning just one _EXPANSION, we
3691 return a TREE_LIST whose TREE_PURPOSE is a
3692 TYPE_PACK_EXPANSION containing the bases that will be
3693 initialized. The TREE_VALUE will be identical to the
3694 original TREE_VALUE, which is a list of arguments that will
3695 be passed to each base. We do not introduce any new pack
3696 expansion nodes into the TREE_VALUE (although it is possible
3697 that some already exist), because the TREE_PURPOSE and
3698 TREE_VALUE all need to be expanded together with the same
3699 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3700 resulting TREE_PURPOSE will mention the parameter packs in
3701 both the bases and the arguments to the bases. */
3702 tree purpose;
3703 tree value;
3704 tree parameter_packs = NULL_TREE;
3705
3706 /* Determine which parameter packs will be used by the base
3707 class expansion. */
3708 ppd.visited = new hash_set<tree>;
3709 ppd.parameter_packs = &parameter_packs;
3710 ppd.type_pack_expansion_p = true;
3711 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3712 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3713 &ppd, ppd.visited);
3714
3715 if (parameter_packs == NULL_TREE)
3716 {
3717 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3718 delete ppd.visited;
3719 return error_mark_node;
3720 }
3721
3722 if (TREE_VALUE (arg) != void_type_node)
3723 {
3724 /* Collect the sets of parameter packs used in each of the
3725 initialization arguments. */
3726 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3727 {
3728 /* Determine which parameter packs will be expanded in this
3729 argument. */
3730 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3731 &ppd, ppd.visited);
3732 }
3733 }
3734
3735 delete ppd.visited;
3736
3737 /* Create the pack expansion type for the base type. */
3738 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3739 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3740 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3741
3742 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3743 they will rarely be compared to anything. */
3744 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3745
3746 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3747 }
3748
3749 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3750 for_types = true;
3751
3752 /* Build the PACK_EXPANSION_* node. */
3753 result = for_types
3754 ? cxx_make_type (TYPE_PACK_EXPANSION)
3755 : make_node (EXPR_PACK_EXPANSION);
3756 SET_PACK_EXPANSION_PATTERN (result, arg);
3757 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3758 {
3759 /* Propagate type and const-expression information. */
3760 TREE_TYPE (result) = TREE_TYPE (arg);
3761 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3762 /* Mark this read now, since the expansion might be length 0. */
3763 mark_exp_read (arg);
3764 }
3765 else
3766 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3767 they will rarely be compared to anything. */
3768 SET_TYPE_STRUCTURAL_EQUALITY (result);
3769
3770 /* Determine which parameter packs will be expanded. */
3771 ppd.parameter_packs = &parameter_packs;
3772 ppd.visited = new hash_set<tree>;
3773 ppd.type_pack_expansion_p = TYPE_P (arg);
3774 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3775 delete ppd.visited;
3776
3777 /* Make sure we found some parameter packs. */
3778 if (parameter_packs == NULL_TREE)
3779 {
3780 if (TYPE_P (arg))
3781 error ("expansion pattern %<%T%> contains no argument packs", arg);
3782 else
3783 error ("expansion pattern %<%E%> contains no argument packs", arg);
3784 return error_mark_node;
3785 }
3786 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3787
3788 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3789
3790 return result;
3791}
3792
3793/* Checks T for any "bare" parameter packs, which have not yet been
3794 expanded, and issues an error if any are found. This operation can
3795 only be done on full expressions or types (e.g., an expression
3796 statement, "if" condition, etc.), because we could have expressions like:
3797
3798 foo(f(g(h(args)))...)
3799
3800 where "args" is a parameter pack. check_for_bare_parameter_packs
3801 should not be called for the subexpressions args, h(args),
3802 g(h(args)), or f(g(h(args))), because we would produce erroneous
3803 error messages.
3804
3805 Returns TRUE and emits an error if there were bare parameter packs,
3806 returns FALSE otherwise. */
3807bool
3808check_for_bare_parameter_packs (tree t)
3809{
3810 tree parameter_packs = NULL_TREE;
3811 struct find_parameter_pack_data ppd;
3812
3813 if (!processing_template_decl || !t || t == error_mark_node)
3814 return false;
3815
3816 if (TREE_CODE (t) == TYPE_DECL)
3817 t = TREE_TYPE (t);
3818
3819 ppd.parameter_packs = &parameter_packs;
3820 ppd.visited = new hash_set<tree>;
3821 ppd.type_pack_expansion_p = false;
3822 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3823 delete ppd.visited;
3824
3825 if (parameter_packs)
3826 {
3827 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3828 error_at (loc, "parameter packs not expanded with %<...%>:");
3829 while (parameter_packs)
3830 {
3831 tree pack = TREE_VALUE (parameter_packs);
3832 tree name = NULL_TREE;
3833
3834 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3835 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3836 name = TYPE_NAME (pack);
3837 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3838 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3839 else
3840 name = DECL_NAME (pack);
3841
3842 if (name)
3843 inform (loc, " %qD", name);
3844 else
3845 inform (loc, " <anonymous>");
3846
3847 parameter_packs = TREE_CHAIN (parameter_packs);
3848 }
3849
3850 return true;
3851 }
3852
3853 return false;
3854}
3855
3856/* Expand any parameter packs that occur in the template arguments in
3857 ARGS. */
3858tree
3859expand_template_argument_pack (tree args)
3860{
3861 if (args == error_mark_node)
3862 return error_mark_node;
3863
3864 tree result_args = NULL_TREE;
3865 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3866 int num_result_args = -1;
3867 int non_default_args_count = -1;
3868
3869 /* First, determine if we need to expand anything, and the number of
3870 slots we'll need. */
3871 for (in_arg = 0; in_arg < nargs; ++in_arg)
3872 {
3873 tree arg = TREE_VEC_ELT (args, in_arg);
3874 if (arg == NULL_TREE)
3875 return args;
3876 if (ARGUMENT_PACK_P (arg))
3877 {
3878 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3879 if (num_result_args < 0)
3880 num_result_args = in_arg + num_packed;
3881 else
3882 num_result_args += num_packed;
3883 }
3884 else
3885 {
3886 if (num_result_args >= 0)
3887 num_result_args++;
3888 }
3889 }
3890
3891 /* If no expansion is necessary, we're done. */
3892 if (num_result_args < 0)
3893 return args;
3894
3895 /* Expand arguments. */
3896 result_args = make_tree_vec (num_result_args);
3897 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3898 non_default_args_count =
3899 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3900 for (in_arg = 0; in_arg < nargs; ++in_arg)
3901 {
3902 tree arg = TREE_VEC_ELT (args, in_arg);
3903 if (ARGUMENT_PACK_P (arg))
3904 {
3905 tree packed = ARGUMENT_PACK_ARGS (arg);
3906 int i, num_packed = TREE_VEC_LENGTH (packed);
3907 for (i = 0; i < num_packed; ++i, ++out_arg)
3908 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3909 if (non_default_args_count > 0)
3910 non_default_args_count += num_packed - 1;
3911 }
3912 else
3913 {
3914 TREE_VEC_ELT (result_args, out_arg) = arg;
3915 ++out_arg;
3916 }
3917 }
3918 if (non_default_args_count >= 0)
3919 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3920 return result_args;
3921}
3922
3923/* Checks if DECL shadows a template parameter.
3924
3925 [temp.local]: A template-parameter shall not be redeclared within its
3926 scope (including nested scopes).
3927
3928 Emits an error and returns TRUE if the DECL shadows a parameter,
3929 returns FALSE otherwise. */
3930
3931bool
3932check_template_shadow (tree decl)
3933{
3934 tree olddecl;
3935
3936 /* If we're not in a template, we can't possibly shadow a template
3937 parameter. */
3938 if (!current_template_parms)
3939 return true;
3940
3941 /* Figure out what we're shadowing. */
3942 if (TREE_CODE (decl) == OVERLOAD)
3943 decl = OVL_CURRENT (decl);
3944 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3945
3946 /* If there's no previous binding for this name, we're not shadowing
3947 anything, let alone a template parameter. */
3948 if (!olddecl)
3949 return true;
3950
3951 /* If we're not shadowing a template parameter, we're done. Note
3952 that OLDDECL might be an OVERLOAD (or perhaps even an
3953 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3954 node. */
3955 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3956 return true;
3957
3958 /* We check for decl != olddecl to avoid bogus errors for using a
3959 name inside a class. We check TPFI to avoid duplicate errors for
3960 inline member templates. */
3961 if (decl == olddecl
3962 || (DECL_TEMPLATE_PARM_P (decl)
3963 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3964 return true;
3965
3966 /* Don't complain about the injected class name, as we've already
3967 complained about the class itself. */
3968 if (DECL_SELF_REFERENCE_P (decl))
3969 return false;
3970
3971 if (DECL_TEMPLATE_PARM_P (decl))
3972 error ("declaration of template parameter %q+D shadows "
3973 "template parameter", decl);
3974 else
3975 error ("declaration of %q+#D shadows template parameter", decl);
3976 inform (DECL_SOURCE_LOCATION (olddecl),
3977 "template parameter %qD declared here", olddecl);
3978 return false;
3979}
3980
3981/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3982 ORIG_LEVEL, DECL, and TYPE. */
3983
3984static tree
3985build_template_parm_index (int index,
3986 int level,
3987 int orig_level,
3988 tree decl,
3989 tree type)
3990{
3991 tree t = make_node (TEMPLATE_PARM_INDEX);
3992 TEMPLATE_PARM_IDX (t) = index;
3993 TEMPLATE_PARM_LEVEL (t) = level;
3994 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3995 TEMPLATE_PARM_DECL (t) = decl;
3996 TREE_TYPE (t) = type;
3997 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3998 TREE_READONLY (t) = TREE_READONLY (decl);
3999
4000 return t;
4001}
4002
4003/* Find the canonical type parameter for the given template type
4004 parameter. Returns the canonical type parameter, which may be TYPE
4005 if no such parameter existed. */
4006
4007static tree
4008canonical_type_parameter (tree type)
4009{
4010 tree list;
4011 int idx = TEMPLATE_TYPE_IDX (type);
4012 if (!canonical_template_parms)
4013 vec_alloc (canonical_template_parms, idx + 1);
4014
4015 if (canonical_template_parms->length () <= (unsigned) idx)
4016 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4017
4018 list = (*canonical_template_parms)[idx];
4019 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4020 list = TREE_CHAIN (list);
4021
4022 if (list)
4023 return TREE_VALUE (list);
4024 else
4025 {
4026 (*canonical_template_parms)[idx]
4027 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4028 return type;
4029 }
4030}
4031
4032/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4033 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4034 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4035 new one is created. */
4036
4037static tree
4038reduce_template_parm_level (tree index, tree type, int levels, tree args,
4039 tsubst_flags_t complain)
4040{
4041 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4042 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4043 != TEMPLATE_PARM_LEVEL (index) - levels)
4044 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4045 {
4046 tree orig_decl = TEMPLATE_PARM_DECL (index);
4047 tree decl, t;
4048
4049 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4050 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4051 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4052 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4053 DECL_ARTIFICIAL (decl) = 1;
4054 SET_DECL_TEMPLATE_PARM_P (decl);
4055
4056 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4057 TEMPLATE_PARM_LEVEL (index) - levels,
4058 TEMPLATE_PARM_ORIG_LEVEL (index),
4059 decl, type);
4060 TEMPLATE_PARM_DESCENDANTS (index) = t;
4061 TEMPLATE_PARM_PARAMETER_PACK (t)
4062 = TEMPLATE_PARM_PARAMETER_PACK (index);
4063
4064 /* Template template parameters need this. */
4065 if (TREE_CODE (decl) == TEMPLATE_DECL)
4066 {
4067 DECL_TEMPLATE_RESULT (decl)
4068 = build_decl (DECL_SOURCE_LOCATION (decl),
4069 TYPE_DECL, DECL_NAME (decl), type);
4070 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4071 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4072 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4073 }
4074 }
4075
4076 return TEMPLATE_PARM_DESCENDANTS (index);
4077}
4078
4079/* Process information from new template parameter PARM and append it
4080 to the LIST being built. This new parameter is a non-type
4081 parameter iff IS_NON_TYPE is true. This new parameter is a
4082 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4083 is in PARM_LOC. */
4084
4085tree
4086process_template_parm (tree list, location_t parm_loc, tree parm,
4087 bool is_non_type, bool is_parameter_pack)
4088{
4089 tree decl = 0;
4090 int idx = 0;
4091
4092 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4093 tree defval = TREE_PURPOSE (parm);
4094 tree constr = TREE_TYPE (parm);
4095
4096 if (list)
4097 {
4098 tree p = tree_last (list);
4099
4100 if (p && TREE_VALUE (p) != error_mark_node)
4101 {
4102 p = TREE_VALUE (p);
4103 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4104 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4105 else
4106 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4107 }
4108
4109 ++idx;
4110 }
4111
4112 if (is_non_type)
4113 {
4114 parm = TREE_VALUE (parm);
4115
4116 SET_DECL_TEMPLATE_PARM_P (parm);
4117
4118 if (TREE_TYPE (parm) != error_mark_node)
4119 {
4120 /* [temp.param]
4121
4122 The top-level cv-qualifiers on the template-parameter are
4123 ignored when determining its type. */
4124 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4125 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4126 TREE_TYPE (parm) = error_mark_node;
4127 else if (uses_parameter_packs (TREE_TYPE (parm))
4128 && !is_parameter_pack
4129 /* If we're in a nested template parameter list, the template
4130 template parameter could be a parameter pack. */
4131 && processing_template_parmlist == 1)
4132 {
4133 /* This template parameter is not a parameter pack, but it
4134 should be. Complain about "bare" parameter packs. */
4135 check_for_bare_parameter_packs (TREE_TYPE (parm));
4136
4137 /* Recover by calling this a parameter pack. */
4138 is_parameter_pack = true;
4139 }
4140 }
4141
4142 /* A template parameter is not modifiable. */
4143 TREE_CONSTANT (parm) = 1;
4144 TREE_READONLY (parm) = 1;
4145 decl = build_decl (parm_loc,
4146 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4147 TREE_CONSTANT (decl) = 1;
4148 TREE_READONLY (decl) = 1;
4149 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4150 = build_template_parm_index (idx, processing_template_decl,
4151 processing_template_decl,
4152 decl, TREE_TYPE (parm));
4153
4154 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4155 = is_parameter_pack;
4156 }
4157 else
4158 {
4159 tree t;
4160 parm = TREE_VALUE (TREE_VALUE (parm));
4161
4162 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4163 {
4164 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4165 /* This is for distinguishing between real templates and template
4166 template parameters */
4167 TREE_TYPE (parm) = t;
4168 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4169 decl = parm;
4170 }
4171 else
4172 {
4173 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4174 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4175 decl = build_decl (parm_loc,
4176 TYPE_DECL, parm, t);
4177 }
4178
4179 TYPE_NAME (t) = decl;
4180 TYPE_STUB_DECL (t) = decl;
4181 parm = decl;
4182 TEMPLATE_TYPE_PARM_INDEX (t)
4183 = build_template_parm_index (idx, processing_template_decl,
4184 processing_template_decl,
4185 decl, TREE_TYPE (parm));
4186 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4187 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4188 }
4189 DECL_ARTIFICIAL (decl) = 1;
4190 SET_DECL_TEMPLATE_PARM_P (decl);
4191
4192 /* Build requirements for the type/template parameter.
4193 This must be done after SET_DECL_TEMPLATE_PARM_P or
4194 process_template_parm could fail. */
4195 tree reqs = finish_shorthand_constraint (parm, constr);
4196
4197 pushdecl (decl);
4198
4199 /* Build the parameter node linking the parameter declaration,
4200 its default argument (if any), and its constraints (if any). */
4201 parm = build_tree_list (defval, parm);
4202 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4203
4204 return chainon (list, parm);
4205}
4206
4207/* The end of a template parameter list has been reached. Process the
4208 tree list into a parameter vector, converting each parameter into a more
4209 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4210 as PARM_DECLs. */
4211
4212tree
4213end_template_parm_list (tree parms)
4214{
4215 int nparms;
4216 tree parm, next;
4217 tree saved_parmlist = make_tree_vec (list_length (parms));
4218
4219 /* Pop the dummy parameter level and add the real one. */
4220 current_template_parms = TREE_CHAIN (current_template_parms);
4221
4222 current_template_parms
4223 = tree_cons (size_int (processing_template_decl),
4224 saved_parmlist, current_template_parms);
4225
4226 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4227 {
4228 next = TREE_CHAIN (parm);
4229 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4230 TREE_CHAIN (parm) = NULL_TREE;
4231 }
4232
4233 --processing_template_parmlist;
4234
4235 return saved_parmlist;
4236}
4237
4238// Explicitly indicate the end of the template parameter list. We assume
4239// that the current template parameters have been constructed and/or
4240// managed explicitly, as when creating new template template parameters
4241// from a shorthand constraint.
4242void
4243end_template_parm_list ()
4244{
4245 --processing_template_parmlist;
4246}
4247
4248/* end_template_decl is called after a template declaration is seen. */
4249
4250void
4251end_template_decl (void)
4252{
4253 reset_specialization ();
4254
4255 if (! processing_template_decl)
4256 return;
4257
4258 /* This matches the pushlevel in begin_template_parm_list. */
4259 finish_scope ();
4260
4261 --processing_template_decl;
4262 current_template_parms = TREE_CHAIN (current_template_parms);
4263}
4264
4265/* Takes a TREE_LIST representing a template parameter and convert it
4266 into an argument suitable to be passed to the type substitution
4267 functions. Note that If the TREE_LIST contains an error_mark
4268 node, the returned argument is error_mark_node. */
4269
4270tree
4271template_parm_to_arg (tree t)
4272{
4273
4274 if (t == NULL_TREE
4275 || TREE_CODE (t) != TREE_LIST)
4276 return t;
4277
4278 if (error_operand_p (TREE_VALUE (t)))
4279 return error_mark_node;
4280
4281 t = TREE_VALUE (t);
4282
4283 if (TREE_CODE (t) == TYPE_DECL
4284 || TREE_CODE (t) == TEMPLATE_DECL)
4285 {
4286 t = TREE_TYPE (t);
4287
4288 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4289 {
4290 /* Turn this argument into a TYPE_ARGUMENT_PACK
4291 with a single element, which expands T. */
4292 tree vec = make_tree_vec (1);
4293 if (CHECKING_P)
4294 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4295
4296 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4297
4298 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4299 SET_ARGUMENT_PACK_ARGS (t, vec);
4300 }
4301 }
4302 else
4303 {
4304 t = DECL_INITIAL (t);
4305
4306 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4307 {
4308 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4309 with a single element, which expands T. */
4310 tree vec = make_tree_vec (1);
4311 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4312 if (CHECKING_P)
4313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4314
4315 t = convert_from_reference (t);
4316 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4317
4318 t = make_node (NONTYPE_ARGUMENT_PACK);
4319 SET_ARGUMENT_PACK_ARGS (t, vec);
4320 TREE_TYPE (t) = type;
4321 }
4322 else
4323 t = convert_from_reference (t);
4324 }
4325 return t;
4326}
4327
4328/* Given a single level of template parameters (a TREE_VEC), return it
4329 as a set of template arguments. */
4330
4331static tree
4332template_parms_level_to_args (tree parms)
4333{
4334 tree a = copy_node (parms);
4335 TREE_TYPE (a) = NULL_TREE;
4336 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4337 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4338
4339 if (CHECKING_P)
4340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4341
4342 return a;
4343}
4344
4345/* Given a set of template parameters, return them as a set of template
4346 arguments. The template parameters are represented as a TREE_VEC, in
4347 the form documented in cp-tree.h for template arguments. */
4348
4349static tree
4350template_parms_to_args (tree parms)
4351{
4352 tree header;
4353 tree args = NULL_TREE;
4354 int length = TMPL_PARMS_DEPTH (parms);
4355 int l = length;
4356
4357 /* If there is only one level of template parameters, we do not
4358 create a TREE_VEC of TREE_VECs. Instead, we return a single
4359 TREE_VEC containing the arguments. */
4360 if (length > 1)
4361 args = make_tree_vec (length);
4362
4363 for (header = parms; header; header = TREE_CHAIN (header))
4364 {
4365 tree a = template_parms_level_to_args (TREE_VALUE (header));
4366
4367 if (length > 1)
4368 TREE_VEC_ELT (args, --l) = a;
4369 else
4370 args = a;
4371 }
4372
4373 return args;
4374}
4375
4376/* Within the declaration of a template, return the currently active
4377 template parameters as an argument TREE_VEC. */
4378
4379static tree
4380current_template_args (void)
4381{
4382 return template_parms_to_args (current_template_parms);
4383}
4384
4385/* Update the declared TYPE by doing any lookups which were thought to be
4386 dependent, but are not now that we know the SCOPE of the declarator. */
4387
4388tree
4389maybe_update_decl_type (tree orig_type, tree scope)
4390{
4391 tree type = orig_type;
4392
4393 if (type == NULL_TREE)
4394 return type;
4395
4396 if (TREE_CODE (orig_type) == TYPE_DECL)
4397 type = TREE_TYPE (type);
4398
4399 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4400 && dependent_type_p (type)
4401 /* Don't bother building up the args in this case. */
4402 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4403 {
4404 /* tsubst in the args corresponding to the template parameters,
4405 including auto if present. Most things will be unchanged, but
4406 make_typename_type and tsubst_qualified_id will resolve
4407 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4408 tree args = current_template_args ();
4409 tree auto_node = type_uses_auto (type);
4410 tree pushed;
4411 if (auto_node)
4412 {
4413 tree auto_vec = make_tree_vec (1);
4414 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4415 args = add_to_template_args (args, auto_vec);
4416 }
4417 pushed = push_scope (scope);
4418 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4419 if (pushed)
4420 pop_scope (scope);
4421 }
4422
4423 if (type == error_mark_node)
4424 return orig_type;
4425
4426 if (TREE_CODE (orig_type) == TYPE_DECL)
4427 {
4428 if (same_type_p (type, TREE_TYPE (orig_type)))
4429 type = orig_type;
4430 else
4431 type = TYPE_NAME (type);
4432 }
4433 return type;
4434}
4435
4436/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4437 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4438 the new template is a member template. */
4439
4440tree
4441build_template_decl (tree decl, tree parms, bool member_template_p)
4442{
4443 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4444 DECL_TEMPLATE_PARMS (tmpl) = parms;
4445 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4446 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4447 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4448
4449 return tmpl;
4450}
4451
4452struct template_parm_data
4453{
4454 /* The level of the template parameters we are currently
4455 processing. */
4456 int level;
4457
4458 /* The index of the specialization argument we are currently
4459 processing. */
4460 int current_arg;
4461
4462 /* An array whose size is the number of template parameters. The
4463 elements are nonzero if the parameter has been used in any one
4464 of the arguments processed so far. */
4465 int* parms;
4466
4467 /* An array whose size is the number of template arguments. The
4468 elements are nonzero if the argument makes use of template
4469 parameters of this level. */
4470 int* arg_uses_template_parms;
4471};
4472
4473/* Subroutine of push_template_decl used to see if each template
4474 parameter in a partial specialization is used in the explicit
4475 argument list. If T is of the LEVEL given in DATA (which is
4476 treated as a template_parm_data*), then DATA->PARMS is marked
4477 appropriately. */
4478
4479static int
4480mark_template_parm (tree t, void* data)
4481{
4482 int level;
4483 int idx;
4484 struct template_parm_data* tpd = (struct template_parm_data*) data;
4485
4486 template_parm_level_and_index (t, &level, &idx);
4487
4488 if (level == tpd->level)
4489 {
4490 tpd->parms[idx] = 1;
4491 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4492 }
4493
4494 /* In C++17 the type of a non-type argument is a deduced context. */
4495 if (cxx_dialect >= cxx1z
4496 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4497 for_each_template_parm (TREE_TYPE (t),
4498 &mark_template_parm,
4499 data,
4500 NULL,
4501 /*include_nondeduced_p=*/false);
4502
4503 /* Return zero so that for_each_template_parm will continue the
4504 traversal of the tree; we want to mark *every* template parm. */
4505 return 0;
4506}
4507
4508/* Process the partial specialization DECL. */
4509
4510static tree
4511process_partial_specialization (tree decl)
4512{
4513 tree type = TREE_TYPE (decl);
4514 tree tinfo = get_template_info (decl);
4515 tree maintmpl = TI_TEMPLATE (tinfo);
4516 tree specargs = TI_ARGS (tinfo);
4517 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4518 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4519 tree inner_parms;
4520 tree inst;
4521 int nargs = TREE_VEC_LENGTH (inner_args);
4522 int ntparms;
4523 int i;
4524 bool did_error_intro = false;
4525 struct template_parm_data tpd;
4526 struct template_parm_data tpd2;
4527
4528 gcc_assert (current_template_parms);
4529
4530 /* A concept cannot be specialized. */
4531 if (flag_concepts && variable_concept_p (maintmpl))
4532 {
4533 error ("specialization of variable concept %q#D", maintmpl);
4534 return error_mark_node;
4535 }
4536
4537 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4538 ntparms = TREE_VEC_LENGTH (inner_parms);
4539
4540 /* We check that each of the template parameters given in the
4541 partial specialization is used in the argument list to the
4542 specialization. For example:
4543
4544 template <class T> struct S;
4545 template <class T> struct S<T*>;
4546
4547 The second declaration is OK because `T*' uses the template
4548 parameter T, whereas
4549
4550 template <class T> struct S<int>;
4551
4552 is no good. Even trickier is:
4553
4554 template <class T>
4555 struct S1
4556 {
4557 template <class U>
4558 struct S2;
4559 template <class U>
4560 struct S2<T>;
4561 };
4562
4563 The S2<T> declaration is actually invalid; it is a
4564 full-specialization. Of course,
4565
4566 template <class U>
4567 struct S2<T (*)(U)>;
4568
4569 or some such would have been OK. */
4570 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4571 tpd.parms = XALLOCAVEC (int, ntparms);
4572 memset (tpd.parms, 0, sizeof (int) * ntparms);
4573
4574 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4575 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4576 for (i = 0; i < nargs; ++i)
4577 {
4578 tpd.current_arg = i;
4579 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4580 &mark_template_parm,
4581 &tpd,
4582 NULL,
4583 /*include_nondeduced_p=*/false);
4584 }
4585 for (i = 0; i < ntparms; ++i)
4586 if (tpd.parms[i] == 0)
4587 {
4588 /* One of the template parms was not used in a deduced context in the
4589 specialization. */
4590 if (!did_error_intro)
4591 {
4592 error ("template parameters not deducible in "
4593 "partial specialization:");
4594 did_error_intro = true;
4595 }
4596
4597 inform (input_location, " %qD",
4598 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4599 }
4600
4601 if (did_error_intro)
4602 return error_mark_node;
4603
4604 /* [temp.class.spec]
4605
4606 The argument list of the specialization shall not be identical to
4607 the implicit argument list of the primary template. */
4608 tree main_args
4609 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4610 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4611 && (!flag_concepts
4612 || !strictly_subsumes (current_template_constraints (),
4613 get_constraints (maintmpl))))
4614 {
4615 if (!flag_concepts)
4616 error ("partial specialization %q+D does not specialize "
4617 "any template arguments; to define the primary template, "
4618 "remove the template argument list", decl);
4619 else
4620 error ("partial specialization %q+D does not specialize any "
4621 "template arguments and is not more constrained than "
4622 "the primary template; to define the primary template, "
4623 "remove the template argument list", decl);
4624 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4625 }
4626
4627 /* A partial specialization that replaces multiple parameters of the
4628 primary template with a pack expansion is less specialized for those
4629 parameters. */
4630 if (nargs < DECL_NTPARMS (maintmpl))
4631 {
4632 error ("partial specialization is not more specialized than the "
4633 "primary template because it replaces multiple parameters "
4634 "with a pack expansion");
4635 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4636 /* Avoid crash in process_partial_specialization. */
4637 return decl;
4638 }
4639
4640 /* If we aren't in a dependent class, we can actually try deduction. */
4641 else if (tpd.level == 1
4642 /* FIXME we should be able to handle a partial specialization of a
4643 partial instantiation, but currently we can't (c++/41727). */
4644 && TMPL_ARGS_DEPTH (specargs) == 1
4645 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4646 {
4647 if (permerror (input_location, "partial specialization %qD is not "
4648 "more specialized than", decl))
4649 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4650 maintmpl);
4651 }
4652
4653 /* [temp.class.spec]
4654
4655 A partially specialized non-type argument expression shall not
4656 involve template parameters of the partial specialization except
4657 when the argument expression is a simple identifier.
4658
4659 The type of a template parameter corresponding to a specialized
4660 non-type argument shall not be dependent on a parameter of the
4661 specialization.
4662
4663 Also, we verify that pack expansions only occur at the
4664 end of the argument list. */
4665 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4666 tpd2.parms = 0;
4667 for (i = 0; i < nargs; ++i)
4668 {
4669 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4670 tree arg = TREE_VEC_ELT (inner_args, i);
4671 tree packed_args = NULL_TREE;
4672 int j, len = 1;
4673
4674 if (ARGUMENT_PACK_P (arg))
4675 {
4676 /* Extract the arguments from the argument pack. We'll be
4677 iterating over these in the following loop. */
4678 packed_args = ARGUMENT_PACK_ARGS (arg);
4679 len = TREE_VEC_LENGTH (packed_args);
4680 }
4681
4682 for (j = 0; j < len; j++)
4683 {
4684 if (packed_args)
4685 /* Get the Jth argument in the parameter pack. */
4686 arg = TREE_VEC_ELT (packed_args, j);
4687
4688 if (PACK_EXPANSION_P (arg))
4689 {
4690 /* Pack expansions must come at the end of the
4691 argument list. */
4692 if ((packed_args && j < len - 1)
4693 || (!packed_args && i < nargs - 1))
4694 {
4695 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4696 error ("parameter pack argument %qE must be at the "
4697 "end of the template argument list", arg);
4698 else
4699 error ("parameter pack argument %qT must be at the "
4700 "end of the template argument list", arg);
4701 }
4702 }
4703
4704 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4705 /* We only care about the pattern. */
4706 arg = PACK_EXPANSION_PATTERN (arg);
4707
4708 if (/* These first two lines are the `non-type' bit. */
4709 !TYPE_P (arg)
4710 && TREE_CODE (arg) != TEMPLATE_DECL
4711 /* This next two lines are the `argument expression is not just a
4712 simple identifier' condition and also the `specialized
4713 non-type argument' bit. */
4714 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4715 && !(REFERENCE_REF_P (arg)
4716 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4717 {
4718 if ((!packed_args && tpd.arg_uses_template_parms[i])
4719 || (packed_args && uses_template_parms (arg)))
4720 error ("template argument %qE involves template parameter(s)",
4721 arg);
4722 else
4723 {
4724 /* Look at the corresponding template parameter,
4725 marking which template parameters its type depends
4726 upon. */
4727 tree type = TREE_TYPE (parm);
4728
4729 if (!tpd2.parms)
4730 {
4731 /* We haven't yet initialized TPD2. Do so now. */
4732 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4733 /* The number of parameters here is the number in the
4734 main template, which, as checked in the assertion
4735 above, is NARGS. */
4736 tpd2.parms = XALLOCAVEC (int, nargs);
4737 tpd2.level =
4738 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4739 }
4740
4741 /* Mark the template parameters. But this time, we're
4742 looking for the template parameters of the main
4743 template, not in the specialization. */
4744 tpd2.current_arg = i;
4745 tpd2.arg_uses_template_parms[i] = 0;
4746 memset (tpd2.parms, 0, sizeof (int) * nargs);
4747 for_each_template_parm (type,
4748 &mark_template_parm,
4749 &tpd2,
4750 NULL,
4751 /*include_nondeduced_p=*/false);
4752
4753 if (tpd2.arg_uses_template_parms [i])
4754 {
4755 /* The type depended on some template parameters.
4756 If they are fully specialized in the
4757 specialization, that's OK. */
4758 int j;
4759 int count = 0;
4760 for (j = 0; j < nargs; ++j)
4761 if (tpd2.parms[j] != 0
4762 && tpd.arg_uses_template_parms [j])
4763 ++count;
4764 if (count != 0)
4765 error_n (input_location, count,
4766 "type %qT of template argument %qE depends "
4767 "on a template parameter",
4768 "type %qT of template argument %qE depends "
4769 "on template parameters",
4770 type,
4771 arg);
4772 }
4773 }
4774 }
4775 }
4776 }
4777
4778 /* We should only get here once. */
4779 if (TREE_CODE (decl) == TYPE_DECL)
4780 gcc_assert (!COMPLETE_TYPE_P (type));
4781
4782 // Build the template decl.
4783 tree tmpl = build_template_decl (decl, current_template_parms,
4784 DECL_MEMBER_TEMPLATE_P (maintmpl));
4785 TREE_TYPE (tmpl) = type;
4786 DECL_TEMPLATE_RESULT (tmpl) = decl;
4787 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4788 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4789 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4790
4791 /* Give template template parms a DECL_CONTEXT of the template
4792 for which they are a parameter. */
4793 for (i = 0; i < ntparms; ++i)
4794 {
4795 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4796 if (TREE_CODE (parm) == TEMPLATE_DECL)
4797 DECL_CONTEXT (parm) = tmpl;
4798 }
4799
4800 if (VAR_P (decl))
4801 /* We didn't register this in check_explicit_specialization so we could
4802 wait until the constraints were set. */
4803 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4804 else
4805 associate_classtype_constraints (type);
4806
4807 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4808 = tree_cons (specargs, tmpl,
4809 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4810 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4811
4812 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4813 inst = TREE_CHAIN (inst))
4814 {
4815 tree instance = TREE_VALUE (inst);
4816 if (TYPE_P (instance)
4817 ? (COMPLETE_TYPE_P (instance)
4818 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4819 : DECL_TEMPLATE_INSTANTIATION (instance))
4820 {
4821 tree spec = most_specialized_partial_spec (instance, tf_none);
4822 tree inst_decl = (DECL_P (instance)
4823 ? instance : TYPE_NAME (instance));
4824 if (!spec)
4825 /* OK */;
4826 else if (spec == error_mark_node)
4827 permerror (input_location,
4828 "declaration of %qD ambiguates earlier template "
4829 "instantiation for %qD", decl, inst_decl);
4830 else if (TREE_VALUE (spec) == tmpl)
4831 permerror (input_location,
4832 "partial specialization of %qD after instantiation "
4833 "of %qD", decl, inst_decl);
4834 }
4835 }
4836
4837 return decl;
4838}
4839
4840/* PARM is a template parameter of some form; return the corresponding
4841 TEMPLATE_PARM_INDEX. */
4842
4843static tree
4844get_template_parm_index (tree parm)
4845{
4846 if (TREE_CODE (parm) == PARM_DECL
4847 || TREE_CODE (parm) == CONST_DECL)
4848 parm = DECL_INITIAL (parm);
4849 else if (TREE_CODE (parm) == TYPE_DECL
4850 || TREE_CODE (parm) == TEMPLATE_DECL)
4851 parm = TREE_TYPE (parm);
4852 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4853 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4854 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4855 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4856 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4857 return parm;
4858}
4859
4860/* Subroutine of fixed_parameter_pack_p below. Look for any template
4861 parameter packs used by the template parameter PARM. */
4862
4863static void
4864fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4865{
4866 /* A type parm can't refer to another parm. */
4867 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
4868 return;
4869 else if (TREE_CODE (parm) == PARM_DECL)
4870 {
4871 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4872 ppd, ppd->visited);
4873 return;
4874 }
4875
4876 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4877
4878 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4879 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4880 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4881}
4882
4883/* PARM is a template parameter pack. Return any parameter packs used in
4884 its type or the type of any of its template parameters. If there are
4885 any such packs, it will be instantiated into a fixed template parameter
4886 list by partial instantiation rather than be fully deduced. */
4887
4888tree
4889fixed_parameter_pack_p (tree parm)
4890{
4891 /* This can only be true in a member template. */
4892 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4893 return NULL_TREE;
4894 /* This can only be true for a parameter pack. */
4895 if (!template_parameter_pack_p (parm))
4896 return NULL_TREE;
4897 /* A type parm can't refer to another parm. */
4898 if (TREE_CODE (parm) == TYPE_DECL)
4899 return NULL_TREE;
4900
4901 tree parameter_packs = NULL_TREE;
4902 struct find_parameter_pack_data ppd;
4903 ppd.parameter_packs = &parameter_packs;
4904 ppd.visited = new hash_set<tree>;
4905 ppd.type_pack_expansion_p = false;
4906
4907 fixed_parameter_pack_p_1 (parm, &ppd);
4908
4909 delete ppd.visited;
4910 return parameter_packs;
4911}
4912
4913/* Check that a template declaration's use of default arguments and
4914 parameter packs is not invalid. Here, PARMS are the template
4915 parameters. IS_PRIMARY is true if DECL is the thing declared by
4916 a primary template. IS_PARTIAL is true if DECL is a partial
4917 specialization.
4918
4919 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4920 declaration (but not a definition); 1 indicates a declaration, 2
4921 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4922 emitted for extraneous default arguments.
4923
4924 Returns TRUE if there were no errors found, FALSE otherwise. */
4925
4926bool
4927check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4928 bool is_partial, int is_friend_decl)
4929{
4930 const char *msg;
4931 int last_level_to_check;
4932 tree parm_level;
4933 bool no_errors = true;
4934
4935 /* [temp.param]
4936
4937 A default template-argument shall not be specified in a
4938 function template declaration or a function template definition, nor
4939 in the template-parameter-list of the definition of a member of a
4940 class template. */
4941
4942 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4943 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4944 /* You can't have a function template declaration in a local
4945 scope, nor you can you define a member of a class template in a
4946 local scope. */
4947 return true;
4948
4949 if ((TREE_CODE (decl) == TYPE_DECL
4950 && TREE_TYPE (decl)
4951 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4952 || (TREE_CODE (decl) == FUNCTION_DECL
4953 && LAMBDA_FUNCTION_P (decl)))
4954 /* A lambda doesn't have an explicit declaration; don't complain
4955 about the parms of the enclosing class. */
4956 return true;
4957
4958 if (current_class_type
4959 && !TYPE_BEING_DEFINED (current_class_type)
4960 && DECL_LANG_SPECIFIC (decl)
4961 && DECL_DECLARES_FUNCTION_P (decl)
4962 /* If this is either a friend defined in the scope of the class
4963 or a member function. */
4964 && (DECL_FUNCTION_MEMBER_P (decl)
4965 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4966 : DECL_FRIEND_CONTEXT (decl)
4967 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4968 : false)
4969 /* And, if it was a member function, it really was defined in
4970 the scope of the class. */
4971 && (!DECL_FUNCTION_MEMBER_P (decl)
4972 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4973 /* We already checked these parameters when the template was
4974 declared, so there's no need to do it again now. This function
4975 was defined in class scope, but we're processing its body now
4976 that the class is complete. */
4977 return true;
4978
4979 /* Core issue 226 (C++0x only): the following only applies to class
4980 templates. */
4981 if (is_primary
4982 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4983 {
4984 /* [temp.param]
4985
4986 If a template-parameter has a default template-argument, all
4987 subsequent template-parameters shall have a default
4988 template-argument supplied. */
4989 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4990 {
4991 tree inner_parms = TREE_VALUE (parm_level);
4992 int ntparms = TREE_VEC_LENGTH (inner_parms);
4993 int seen_def_arg_p = 0;
4994 int i;
4995
4996 for (i = 0; i < ntparms; ++i)
4997 {
4998 tree parm = TREE_VEC_ELT (inner_parms, i);
4999
5000 if (parm == error_mark_node)
5001 continue;
5002
5003 if (TREE_PURPOSE (parm))
5004 seen_def_arg_p = 1;
5005 else if (seen_def_arg_p
5006 && !template_parameter_pack_p (TREE_VALUE (parm)))
5007 {
5008 error ("no default argument for %qD", TREE_VALUE (parm));
5009 /* For better subsequent error-recovery, we indicate that
5010 there should have been a default argument. */
5011 TREE_PURPOSE (parm) = error_mark_node;
5012 no_errors = false;
5013 }
5014 else if (!is_partial
5015 && !is_friend_decl
5016 /* Don't complain about an enclosing partial
5017 specialization. */
5018 && parm_level == parms
5019 && TREE_CODE (decl) == TYPE_DECL
5020 && i < ntparms - 1
5021 && template_parameter_pack_p (TREE_VALUE (parm))
5022 /* A fixed parameter pack will be partially
5023 instantiated into a fixed length list. */
5024 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5025 {
5026 /* A primary class template can only have one
5027 parameter pack, at the end of the template
5028 parameter list. */
5029
5030 error ("parameter pack %q+D must be at the end of the"
5031 " template parameter list", TREE_VALUE (parm));
5032
5033 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5034 = error_mark_node;
5035 no_errors = false;
5036 }
5037 }
5038 }
5039 }
5040
5041 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5042 || is_partial
5043 || !is_primary
5044 || is_friend_decl)
5045 /* For an ordinary class template, default template arguments are
5046 allowed at the innermost level, e.g.:
5047 template <class T = int>
5048 struct S {};
5049 but, in a partial specialization, they're not allowed even
5050 there, as we have in [temp.class.spec]:
5051
5052 The template parameter list of a specialization shall not
5053 contain default template argument values.
5054
5055 So, for a partial specialization, or for a function template
5056 (in C++98/C++03), we look at all of them. */
5057 ;
5058 else
5059 /* But, for a primary class template that is not a partial
5060 specialization we look at all template parameters except the
5061 innermost ones. */
5062 parms = TREE_CHAIN (parms);
5063
5064 /* Figure out what error message to issue. */
5065 if (is_friend_decl == 2)
5066 msg = G_("default template arguments may not be used in function template "
5067 "friend re-declaration");
5068 else if (is_friend_decl)
5069 msg = G_("default template arguments may not be used in function template "
5070 "friend declarations");
5071 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5072 msg = G_("default template arguments may not be used in function templates "
5073 "without -std=c++11 or -std=gnu++11");
5074 else if (is_partial)
5075 msg = G_("default template arguments may not be used in "
5076 "partial specializations");
5077 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5078 msg = G_("default argument for template parameter for class enclosing %qD");
5079 else
5080 /* Per [temp.param]/9, "A default template-argument shall not be
5081 specified in the template-parameter-lists of the definition of
5082 a member of a class template that appears outside of the member's
5083 class.", thus if we aren't handling a member of a class template
5084 there is no need to examine the parameters. */
5085 return true;
5086
5087 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5088 /* If we're inside a class definition, there's no need to
5089 examine the parameters to the class itself. On the one
5090 hand, they will be checked when the class is defined, and,
5091 on the other, default arguments are valid in things like:
5092 template <class T = double>
5093 struct S { template <class U> void f(U); };
5094 Here the default argument for `S' has no bearing on the
5095 declaration of `f'. */
5096 last_level_to_check = template_class_depth (current_class_type) + 1;
5097 else
5098 /* Check everything. */
5099 last_level_to_check = 0;
5100
5101 for (parm_level = parms;
5102 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5103 parm_level = TREE_CHAIN (parm_level))
5104 {
5105 tree inner_parms = TREE_VALUE (parm_level);
5106 int i;
5107 int ntparms;
5108
5109 ntparms = TREE_VEC_LENGTH (inner_parms);
5110 for (i = 0; i < ntparms; ++i)
5111 {
5112 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5113 continue;
5114
5115 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5116 {
5117 if (msg)
5118 {
5119 no_errors = false;
5120 if (is_friend_decl == 2)
5121 return no_errors;
5122
5123 error (msg, decl);
5124 msg = 0;
5125 }
5126
5127 /* Clear out the default argument so that we are not
5128 confused later. */
5129 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5130 }
5131 }
5132
5133 /* At this point, if we're still interested in issuing messages,
5134 they must apply to classes surrounding the object declared. */
5135 if (msg)
5136 msg = G_("default argument for template parameter for class "
5137 "enclosing %qD");
5138 }
5139
5140 return no_errors;
5141}
5142
5143/* Worker for push_template_decl_real, called via
5144 for_each_template_parm. DATA is really an int, indicating the
5145 level of the parameters we are interested in. If T is a template
5146 parameter of that level, return nonzero. */
5147
5148static int
5149template_parm_this_level_p (tree t, void* data)
5150{
5151 int this_level = *(int *)data;
5152 int level;
5153
5154 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5155 level = TEMPLATE_PARM_LEVEL (t);
5156 else
5157 level = TEMPLATE_TYPE_LEVEL (t);
5158 return level == this_level;
5159}
5160
5161/* Worker for uses_outer_template_parms, called via for_each_template_parm.
5162 DATA is really an int, indicating the innermost outer level of parameters.
5163 If T is a template parameter of that level or further out, return
5164 nonzero. */
5165
5166static int
5167template_parm_outer_level (tree t, void *data)
5168{
5169 int this_level = *(int *)data;
5170 int level;
5171
5172 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5173 level = TEMPLATE_PARM_LEVEL (t);
5174 else
5175 level = TEMPLATE_TYPE_LEVEL (t);
5176 return level <= this_level;
5177}
5178
5179/* Creates a TEMPLATE_DECL for the indicated DECL using the template
5180 parameters given by current_template_args, or reuses a
5181 previously existing one, if appropriate. Returns the DECL, or an
5182 equivalent one, if it is replaced via a call to duplicate_decls.
5183
5184 If IS_FRIEND is true, DECL is a friend declaration. */
5185
5186tree
5187push_template_decl_real (tree decl, bool is_friend)
5188{
5189 tree tmpl;
5190 tree args;
5191 tree info;
5192 tree ctx;
5193 bool is_primary;
5194 bool is_partial;
5195 int new_template_p = 0;
5196 /* True if the template is a member template, in the sense of
5197 [temp.mem]. */
5198 bool member_template_p = false;
5199
5200 if (decl == error_mark_node || !current_template_parms)
5201 return error_mark_node;
5202
5203 /* See if this is a partial specialization. */
5204 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5205 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5206 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5207 || (VAR_P (decl)
5208 && DECL_LANG_SPECIFIC (decl)
5209 && DECL_TEMPLATE_SPECIALIZATION (decl)
5210 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5211
5212 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5213 is_friend = true;
5214
5215 if (is_friend)
5216 /* For a friend, we want the context of the friend function, not
5217 the type of which it is a friend. */
5218 ctx = CP_DECL_CONTEXT (decl);
5219 else if (CP_DECL_CONTEXT (decl)
5220 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5221 /* In the case of a virtual function, we want the class in which
5222 it is defined. */
5223 ctx = CP_DECL_CONTEXT (decl);
5224 else
5225 /* Otherwise, if we're currently defining some class, the DECL
5226 is assumed to be a member of the class. */
5227 ctx = current_scope ();
5228
5229 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5230 ctx = NULL_TREE;
5231
5232 if (!DECL_CONTEXT (decl))
5233 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5234
5235 /* See if this is a primary template. */
5236 if (is_friend && ctx
5237 && uses_template_parms_level (ctx, processing_template_decl))
5238 /* A friend template that specifies a class context, i.e.
5239 template <typename T> friend void A<T>::f();
5240 is not primary. */
5241 is_primary = false;
5242 else if (TREE_CODE (decl) == TYPE_DECL
5243 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5244 is_primary = false;
5245 else
5246 is_primary = template_parm_scope_p ();
5247
5248 if (is_primary)
5249 {
5250 warning (OPT_Wtemplates, "template %qD declared", decl);
5251
5252 if (DECL_CLASS_SCOPE_P (decl))
5253 member_template_p = true;
5254 if (TREE_CODE (decl) == TYPE_DECL
5255 && anon_aggrname_p (DECL_NAME (decl)))
5256 {
5257 error ("template class without a name");
5258 return error_mark_node;
5259 }
5260 else if (TREE_CODE (decl) == FUNCTION_DECL)
5261 {
5262 if (member_template_p)
5263 {
5264 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5265 error ("member template %qD may not have virt-specifiers", decl);
5266 }
5267 if (DECL_DESTRUCTOR_P (decl))
5268 {
5269 /* [temp.mem]
5270
5271 A destructor shall not be a member template. */
5272 error ("destructor %qD declared as member template", decl);
5273 return error_mark_node;
5274 }
5275 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5276 && (!prototype_p (TREE_TYPE (decl))
5277 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5278 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5279 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5280 == void_list_node)))
5281 {
5282 /* [basic.stc.dynamic.allocation]
5283
5284 An allocation function can be a function
5285 template. ... Template allocation functions shall
5286 have two or more parameters. */
5287 error ("invalid template declaration of %qD", decl);
5288 return error_mark_node;
5289 }
5290 }
5291 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5292 && CLASS_TYPE_P (TREE_TYPE (decl)))
5293 {
5294 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5295 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5296 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5297 {
5298 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5299 if (TREE_CODE (t) == TYPE_DECL)
5300 t = TREE_TYPE (t);
5301 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5302 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5303 }
5304 }
5305 else if (TREE_CODE (decl) == TYPE_DECL
5306 && TYPE_DECL_ALIAS_P (decl))
5307 /* alias-declaration */
5308 gcc_assert (!DECL_ARTIFICIAL (decl));
5309 else if (VAR_P (decl))
5310 /* C++14 variable template. */;
5311 else
5312 {
5313 error ("template declaration of %q#D", decl);
5314 return error_mark_node;
5315 }
5316 }
5317
5318 /* Check to see that the rules regarding the use of default
5319 arguments are not being violated. */
5320 check_default_tmpl_args (decl, current_template_parms,
5321 is_primary, is_partial, /*is_friend_decl=*/0);
5322
5323 /* Ensure that there are no parameter packs in the type of this
5324 declaration that have not been expanded. */
5325 if (TREE_CODE (decl) == FUNCTION_DECL)
5326 {
5327 /* Check each of the arguments individually to see if there are
5328 any bare parameter packs. */
5329 tree type = TREE_TYPE (decl);
5330 tree arg = DECL_ARGUMENTS (decl);
5331 tree argtype = TYPE_ARG_TYPES (type);
5332
5333 while (arg && argtype)
5334 {
5335 if (!DECL_PACK_P (arg)
5336 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5337 {
5338 /* This is a PARM_DECL that contains unexpanded parameter
5339 packs. We have already complained about this in the
5340 check_for_bare_parameter_packs call, so just replace
5341 these types with ERROR_MARK_NODE. */
5342 TREE_TYPE (arg) = error_mark_node;
5343 TREE_VALUE (argtype) = error_mark_node;
5344 }
5345
5346 arg = DECL_CHAIN (arg);
5347 argtype = TREE_CHAIN (argtype);
5348 }
5349
5350 /* Check for bare parameter packs in the return type and the
5351 exception specifiers. */
5352 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5353 /* Errors were already issued, set return type to int
5354 as the frontend doesn't expect error_mark_node as
5355 the return type. */
5356 TREE_TYPE (type) = integer_type_node;
5357 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5358 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5359 }
5360 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5361 && TYPE_DECL_ALIAS_P (decl))
5362 ? DECL_ORIGINAL_TYPE (decl)
5363 : TREE_TYPE (decl)))
5364 {
5365 TREE_TYPE (decl) = error_mark_node;
5366 return error_mark_node;
5367 }
5368
5369 if (is_partial)
5370 return process_partial_specialization (decl);
5371
5372 args = current_template_args ();
5373
5374 if (!ctx
5375 || TREE_CODE (ctx) == FUNCTION_DECL
5376 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5377 || (TREE_CODE (decl) == TYPE_DECL
5378 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5379 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5380 {
5381 if (DECL_LANG_SPECIFIC (decl)
5382 && DECL_TEMPLATE_INFO (decl)
5383 && DECL_TI_TEMPLATE (decl))
5384 tmpl = DECL_TI_TEMPLATE (decl);
5385 /* If DECL is a TYPE_DECL for a class-template, then there won't
5386 be DECL_LANG_SPECIFIC. The information equivalent to
5387 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5388 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5389 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5390 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5391 {
5392 /* Since a template declaration already existed for this
5393 class-type, we must be redeclaring it here. Make sure
5394 that the redeclaration is valid. */
5395 redeclare_class_template (TREE_TYPE (decl),
5396 current_template_parms,
5397 current_template_constraints ());
5398 /* We don't need to create a new TEMPLATE_DECL; just use the
5399 one we already had. */
5400 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5401 }
5402 else
5403 {
5404 tmpl = build_template_decl (decl, current_template_parms,
5405 member_template_p);
5406 new_template_p = 1;
5407
5408 if (DECL_LANG_SPECIFIC (decl)
5409 && DECL_TEMPLATE_SPECIALIZATION (decl))
5410 {
5411 /* A specialization of a member template of a template
5412 class. */
5413 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5414 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5415 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5416 }
5417 }
5418 }
5419 else
5420 {
5421 tree a, t, current, parms;
5422 int i;
5423 tree tinfo = get_template_info (decl);
5424
5425 if (!tinfo)
5426 {
5427 error ("template definition of non-template %q#D", decl);
5428 return error_mark_node;
5429 }
5430
5431 tmpl = TI_TEMPLATE (tinfo);
5432
5433 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5434 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5435 && DECL_TEMPLATE_SPECIALIZATION (decl)
5436 && DECL_MEMBER_TEMPLATE_P (tmpl))
5437 {
5438 tree new_tmpl;
5439
5440 /* The declaration is a specialization of a member
5441 template, declared outside the class. Therefore, the
5442 innermost template arguments will be NULL, so we
5443 replace them with the arguments determined by the
5444 earlier call to check_explicit_specialization. */
5445 args = DECL_TI_ARGS (decl);
5446
5447 new_tmpl
5448 = build_template_decl (decl, current_template_parms,
5449 member_template_p);
5450 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5451 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5452 DECL_TI_TEMPLATE (decl) = new_tmpl;
5453 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5454 DECL_TEMPLATE_INFO (new_tmpl)
5455 = build_template_info (tmpl, args);
5456
5457 register_specialization (new_tmpl,
5458 most_general_template (tmpl),
5459 args,
5460 is_friend, 0);
5461 return decl;
5462 }
5463
5464 /* Make sure the template headers we got make sense. */
5465
5466 parms = DECL_TEMPLATE_PARMS (tmpl);
5467 i = TMPL_PARMS_DEPTH (parms);
5468 if (TMPL_ARGS_DEPTH (args) != i)
5469 {
5470 error ("expected %d levels of template parms for %q#D, got %d",
5471 i, decl, TMPL_ARGS_DEPTH (args));
5472 DECL_INTERFACE_KNOWN (decl) = 1;
5473 return error_mark_node;
5474 }
5475 else
5476 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5477 {
5478 a = TMPL_ARGS_LEVEL (args, i);
5479 t = INNERMOST_TEMPLATE_PARMS (parms);
5480
5481 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5482 {
5483 if (current == decl)
5484 error ("got %d template parameters for %q#D",
5485 TREE_VEC_LENGTH (a), decl);
5486 else
5487 error ("got %d template parameters for %q#T",
5488 TREE_VEC_LENGTH (a), current);
5489 error (" but %d required", TREE_VEC_LENGTH (t));
5490 /* Avoid crash in import_export_decl. */
5491 DECL_INTERFACE_KNOWN (decl) = 1;
5492 return error_mark_node;
5493 }
5494
5495 if (current == decl)
5496 current = ctx;
5497 else if (current == NULL_TREE)
5498 /* Can happen in erroneous input. */
5499 break;
5500 else
5501 current = get_containing_scope (current);
5502 }
5503
5504 /* Check that the parms are used in the appropriate qualifying scopes
5505 in the declarator. */
5506 if (!comp_template_args
5507 (TI_ARGS (tinfo),
5508 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5509 {
5510 error ("\
5511template arguments to %qD do not match original template %qD",
5512 decl, DECL_TEMPLATE_RESULT (tmpl));
5513 if (!uses_template_parms (TI_ARGS (tinfo)))
5514 inform (input_location, "use template<> for an explicit specialization");
5515 /* Avoid crash in import_export_decl. */
5516 DECL_INTERFACE_KNOWN (decl) = 1;
5517 return error_mark_node;
5518 }
5519 }
5520
5521 DECL_TEMPLATE_RESULT (tmpl) = decl;
5522 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5523
5524 /* Push template declarations for global functions and types. Note
5525 that we do not try to push a global template friend declared in a
5526 template class; such a thing may well depend on the template
5527 parameters of the class. */
5528 if (new_template_p && !ctx
5529 && !(is_friend && template_class_depth (current_class_type) > 0))
5530 {
5531 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5532 if (tmpl == error_mark_node)
5533 return error_mark_node;
5534
5535 /* Hide template friend classes that haven't been declared yet. */
5536 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5537 {
5538 DECL_ANTICIPATED (tmpl) = 1;
5539 DECL_FRIEND_P (tmpl) = 1;
5540 }
5541 }
5542
5543 if (is_primary)
5544 {
5545 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5546 int i;
5547
5548 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5549 if (DECL_CONV_FN_P (tmpl))
5550 {
5551 int depth = TMPL_PARMS_DEPTH (parms);
5552
5553 /* It is a conversion operator. See if the type converted to
5554 depends on innermost template operands. */
5555
5556 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5557 depth))
5558 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5559 }
5560
5561 /* Give template template parms a DECL_CONTEXT of the template
5562 for which they are a parameter. */
5563 parms = INNERMOST_TEMPLATE_PARMS (parms);
5564 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5565 {
5566 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5567 if (TREE_CODE (parm) == TEMPLATE_DECL)
5568 DECL_CONTEXT (parm) = tmpl;
5569 }
5570
5571 if (TREE_CODE (decl) == TYPE_DECL
5572 && TYPE_DECL_ALIAS_P (decl)
5573 && complex_alias_template_p (tmpl))
5574 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5575 }
5576
5577 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5578 back to its most general template. If TMPL is a specialization,
5579 ARGS may only have the innermost set of arguments. Add the missing
5580 argument levels if necessary. */
5581 if (DECL_TEMPLATE_INFO (tmpl))
5582 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5583
5584 info = build_template_info (tmpl, args);
5585
5586 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5587 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5588 else
5589 {
5590 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5591 retrofit_lang_decl (decl);
5592 if (DECL_LANG_SPECIFIC (decl))
5593 DECL_TEMPLATE_INFO (decl) = info;
5594 }
5595
5596 if (flag_implicit_templates
5597 && !is_friend
5598 && TREE_PUBLIC (decl)
5599 && VAR_OR_FUNCTION_DECL_P (decl))
5600 /* Set DECL_COMDAT on template instantiations; if we force
5601 them to be emitted by explicit instantiation or -frepo,
5602 mark_needed will tell cgraph to do the right thing. */
5603 DECL_COMDAT (decl) = true;
5604
5605 return DECL_TEMPLATE_RESULT (tmpl);
5606}
5607
5608tree
5609push_template_decl (tree decl)
5610{
5611 return push_template_decl_real (decl, false);
5612}
5613
5614/* FN is an inheriting constructor that inherits from the constructor
5615 template INHERITED; turn FN into a constructor template with a matching
5616 template header. */
5617
5618tree
5619add_inherited_template_parms (tree fn, tree inherited)
5620{
5621 tree inner_parms
5622 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5623 inner_parms = copy_node (inner_parms);
5624 tree parms
5625 = tree_cons (size_int (processing_template_decl + 1),
5626 inner_parms, current_template_parms);
5627 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5628 tree args = template_parms_to_args (parms);
5629 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5630 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5631 DECL_TEMPLATE_RESULT (tmpl) = fn;
5632 DECL_ARTIFICIAL (tmpl) = true;
5633 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5634 return tmpl;
5635}
5636
5637/* Called when a class template TYPE is redeclared with the indicated
5638 template PARMS, e.g.:
5639
5640 template <class T> struct S;
5641 template <class T> struct S {}; */
5642
5643bool
5644redeclare_class_template (tree type, tree parms, tree cons)
5645{
5646 tree tmpl;
5647 tree tmpl_parms;
5648 int i;
5649
5650 if (!TYPE_TEMPLATE_INFO (type))
5651 {
5652 error ("%qT is not a template type", type);
5653 return false;
5654 }
5655
5656 tmpl = TYPE_TI_TEMPLATE (type);
5657 if (!PRIMARY_TEMPLATE_P (tmpl))
5658 /* The type is nested in some template class. Nothing to worry
5659 about here; there are no new template parameters for the nested
5660 type. */
5661 return true;
5662
5663 if (!parms)
5664 {
5665 error ("template specifiers not specified in declaration of %qD",
5666 tmpl);
5667 return false;
5668 }
5669
5670 parms = INNERMOST_TEMPLATE_PARMS (parms);
5671 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5672
5673 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5674 {
5675 error_n (input_location, TREE_VEC_LENGTH (parms),
5676 "redeclared with %d template parameter",
5677 "redeclared with %d template parameters",
5678 TREE_VEC_LENGTH (parms));
5679 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5680 "previous declaration %qD used %d template parameter",
5681 "previous declaration %qD used %d template parameters",
5682 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5683 return false;
5684 }
5685
5686 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5687 {
5688 tree tmpl_parm;
5689 tree parm;
5690 tree tmpl_default;
5691 tree parm_default;
5692
5693 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5694 || TREE_VEC_ELT (parms, i) == error_mark_node)
5695 continue;
5696
5697 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5698 if (error_operand_p (tmpl_parm))
5699 return false;
5700
5701 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5702 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5703 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5704
5705 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5706 TEMPLATE_DECL. */
5707 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5708 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5709 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5710 || (TREE_CODE (tmpl_parm) != PARM_DECL
5711 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5712 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5713 || (TREE_CODE (tmpl_parm) == PARM_DECL
5714 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5715 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5716 {
5717 error ("template parameter %q+#D", tmpl_parm);
5718 error ("redeclared here as %q#D", parm);
5719 return false;
5720 }
5721
5722 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5723 {
5724 /* We have in [temp.param]:
5725
5726 A template-parameter may not be given default arguments
5727 by two different declarations in the same scope. */
5728 error_at (input_location, "redefinition of default argument for %q#D", parm);
5729 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5730 "original definition appeared here");
5731 return false;
5732 }
5733
5734 if (parm_default != NULL_TREE)
5735 /* Update the previous template parameters (which are the ones
5736 that will really count) with the new default value. */
5737 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5738 else if (tmpl_default != NULL_TREE)
5739 /* Update the new parameters, too; they'll be used as the
5740 parameters for any members. */
5741 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5742
5743 /* Give each template template parm in this redeclaration a
5744 DECL_CONTEXT of the template for which they are a parameter. */
5745 if (TREE_CODE (parm) == TEMPLATE_DECL)
5746 {
5747 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5748 DECL_CONTEXT (parm) = tmpl;
5749 }
5750
5751 if (TREE_CODE (parm) == TYPE_DECL)
5752 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5753 }
5754
5755 // Cannot redeclare a class template with a different set of constraints.
5756 if (!equivalent_constraints (get_constraints (tmpl), cons))
5757 {
5758 error_at (input_location, "redeclaration %q#D with different "
5759 "constraints", tmpl);
5760 inform (DECL_SOURCE_LOCATION (tmpl),
5761 "original declaration appeared here");
5762 }
5763
5764 return true;
5765}
5766
5767/* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5768 to be used when the caller has already checked
5769 (processing_template_decl
5770 && !instantiation_dependent_expression_p (expr)
5771 && potential_constant_expression (expr))
5772 and cleared processing_template_decl. */
5773
5774tree
5775instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5776{
5777 return tsubst_copy_and_build (expr,
5778 /*args=*/NULL_TREE,
5779 complain,
5780 /*in_decl=*/NULL_TREE,
5781 /*function_p=*/false,
5782 /*integral_constant_expression_p=*/true);
5783}
5784
5785/* Simplify EXPR if it is a non-dependent expression. Returns the
5786 (possibly simplified) expression. */
5787
5788tree
5789instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5790{
5791 if (expr == NULL_TREE)
5792 return NULL_TREE;
5793
5794 /* If we're in a template, but EXPR isn't value dependent, simplify
5795 it. We're supposed to treat:
5796
5797 template <typename T> void f(T[1 + 1]);
5798 template <typename T> void f(T[2]);
5799
5800 as two declarations of the same function, for example. */
5801 if (processing_template_decl
5802 && potential_nondependent_constant_expression (expr))
5803 {
5804 processing_template_decl_sentinel s;
5805 expr = instantiate_non_dependent_expr_internal (expr, complain);
5806 }
5807 return expr;
5808}
5809
5810tree
5811instantiate_non_dependent_expr (tree expr)
5812{
5813 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5814}
5815
5816/* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5817 an uninstantiated expression. */
5818
5819tree
5820instantiate_non_dependent_or_null (tree expr)
5821{
5822 if (expr == NULL_TREE)
5823 return NULL_TREE;
5824 if (processing_template_decl)
5825 {
5826 if (!potential_nondependent_constant_expression (expr))
5827 expr = NULL_TREE;
5828 else
5829 {
5830 processing_template_decl_sentinel s;
5831 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5832 }
5833 }
5834 return expr;
5835}
5836
5837/* True iff T is a specialization of a variable template. */
5838
5839bool
5840variable_template_specialization_p (tree t)
5841{
5842 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5843 return false;
5844 tree tmpl = DECL_TI_TEMPLATE (t);
5845 return variable_template_p (tmpl);
5846}
5847
5848/* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5849 template declaration, or a TYPE_DECL for an alias declaration. */
5850
5851bool
5852alias_type_or_template_p (tree t)
5853{
5854 if (t == NULL_TREE)
5855 return false;
5856 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5857 || (TYPE_P (t)
5858 && TYPE_NAME (t)
5859 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5860 || DECL_ALIAS_TEMPLATE_P (t));
5861}
5862
5863/* Return TRUE iff T is a specialization of an alias template. */
5864
5865bool
5866alias_template_specialization_p (const_tree t)
5867{
5868 /* It's an alias template specialization if it's an alias and its
5869 TYPE_NAME is a specialization of a primary template. */
5870 if (TYPE_ALIAS_P (t))
5871 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5872 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5873
5874 return false;
5875}
5876
5877/* An alias template is complex from a SFINAE perspective if a template-id
5878 using that alias can be ill-formed when the expansion is not, as with
5879 the void_t template. We determine this by checking whether the
5880 expansion for the alias template uses all its template parameters. */
5881
5882struct uses_all_template_parms_data
5883{
5884 int level;
5885 bool *seen;
5886};
5887
5888static int
5889uses_all_template_parms_r (tree t, void *data_)
5890{
5891 struct uses_all_template_parms_data &data
5892 = *(struct uses_all_template_parms_data*)data_;
5893 tree idx = get_template_parm_index (t);
5894
5895 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5896 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5897 return 0;
5898}
5899
5900static bool
5901complex_alias_template_p (const_tree tmpl)
5902{
5903 struct uses_all_template_parms_data data;
5904 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5905 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5906 data.level = TMPL_PARMS_DEPTH (parms);
5907 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5908 data.seen = XALLOCAVEC (bool, len);
5909 for (int i = 0; i < len; ++i)
5910 data.seen[i] = false;
5911
5912 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5913 for (int i = 0; i < len; ++i)
5914 if (!data.seen[i])
5915 return true;
5916 return false;
5917}
5918
5919/* Return TRUE iff T is a specialization of a complex alias template with
5920 dependent template-arguments. */
5921
5922bool
5923dependent_alias_template_spec_p (const_tree t)
5924{
5925 if (!alias_template_specialization_p (t))
5926 return false;
5927
5928 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5929 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5930 return false;
5931
5932 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5933 if (!any_dependent_template_arguments_p (args))
5934 return false;
5935
5936 return true;
5937}
5938
5939/* Return the number of innermost template parameters in TMPL. */
5940
5941static int
5942num_innermost_template_parms (tree tmpl)
5943{
5944 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5945 return TREE_VEC_LENGTH (parms);
5946}
5947
5948/* Return either TMPL or another template that it is equivalent to under DR
5949 1286: An alias that just changes the name of a template is equivalent to
5950 the other template. */
5951
5952static tree
5953get_underlying_template (tree tmpl)
5954{
5955 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5956 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5957 {
5958 /* Determine if the alias is equivalent to an underlying template. */
5959 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5960 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5961 if (!tinfo)
5962 break;
5963
5964 tree underlying = TI_TEMPLATE (tinfo);
5965 if (!PRIMARY_TEMPLATE_P (underlying)
5966 || (num_innermost_template_parms (tmpl)
5967 != num_innermost_template_parms (underlying)))
5968 break;
5969
5970 tree alias_args = INNERMOST_TEMPLATE_ARGS
5971 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5972 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5973 break;
5974
5975 /* Alias is equivalent. Strip it and repeat. */
5976 tmpl = underlying;
5977 }
5978
5979 return tmpl;
5980}
5981
5982/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5983 must be a reference-to-function or a pointer-to-function type, as specified
5984 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5985 and check that the resulting function has external linkage. */
5986
5987static tree
5988convert_nontype_argument_function (tree type, tree expr,
5989 tsubst_flags_t complain)
5990{
5991 tree fns = expr;
5992 tree fn, fn_no_ptr;
5993 linkage_kind linkage;
5994
5995 fn = instantiate_type (type, fns, tf_none);
5996 if (fn == error_mark_node)
5997 return error_mark_node;
5998
5999 if (value_dependent_expression_p (fn))
6000 goto accept;
6001
6002 fn_no_ptr = strip_fnptr_conv (fn);
6003 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6004 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6005 if (BASELINK_P (fn_no_ptr))
6006 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6007
6008 /* [temp.arg.nontype]/1
6009
6010 A template-argument for a non-type, non-template template-parameter
6011 shall be one of:
6012 [...]
6013 -- the address of an object or function with external [C++11: or
6014 internal] linkage. */
6015
6016 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6017 {
6018 if (complain & tf_error)
6019 {
6020 error ("%qE is not a valid template argument for type %qT",
6021 expr, type);
6022 if (TYPE_PTR_P (type))
6023 inform (input_location, "it must be the address of a function "
6024 "with external linkage");
6025 else
6026 inform (input_location, "it must be the name of a function with "
6027 "external linkage");
6028 }
6029 return NULL_TREE;
6030 }
6031
6032 linkage = decl_linkage (fn_no_ptr);
6033 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6034 {
6035 if (complain & tf_error)
6036 {
6037 if (cxx_dialect >= cxx11)
6038 error ("%qE is not a valid template argument for type %qT "
6039 "because %qD has no linkage",
6040 expr, type, fn_no_ptr);
6041 else
6042 error ("%qE is not a valid template argument for type %qT "
6043 "because %qD does not have external linkage",
6044 expr, type, fn_no_ptr);
6045 }
6046 return NULL_TREE;
6047 }
6048
6049 accept:
6050 if (TREE_CODE (type) == REFERENCE_TYPE)
6051 {
6052 if (REFERENCE_REF_P (fn))
6053 fn = TREE_OPERAND (fn, 0);
6054 else
6055 fn = build_address (fn);
6056 }
6057 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6058 fn = build_nop (type, fn);
6059
6060 return fn;
6061}
6062
6063/* Subroutine of convert_nontype_argument.
6064 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6065 Emit an error otherwise. */
6066
6067static bool
6068check_valid_ptrmem_cst_expr (tree type, tree expr,
6069 tsubst_flags_t complain)
6070{
6071 STRIP_NOPS (expr);
6072 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6073 return true;
6074 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6075 return true;
6076 if (processing_template_decl
6077 && TREE_CODE (expr) == ADDR_EXPR
6078 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6079 return true;
6080 if (complain & tf_error)
6081 {
6082 error ("%qE is not a valid template argument for type %qT",
6083 expr, type);
6084 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6085 }
6086 return false;
6087}
6088
6089/* Returns TRUE iff the address of OP is value-dependent.
6090
6091 14.6.2.4 [temp.dep.temp]:
6092 A non-integral non-type template-argument is dependent if its type is
6093 dependent or it has either of the following forms
6094 qualified-id
6095 & qualified-id
6096 and contains a nested-name-specifier which specifies a class-name that
6097 names a dependent type.
6098
6099 We generalize this to just say that the address of a member of a
6100 dependent class is value-dependent; the above doesn't cover the
6101 address of a static data member named with an unqualified-id. */
6102
6103static bool
6104has_value_dependent_address (tree op)
6105{
6106 /* We could use get_inner_reference here, but there's no need;
6107 this is only relevant for template non-type arguments, which
6108 can only be expressed as &id-expression. */
6109 if (DECL_P (op))
6110 {
6111 tree ctx = CP_DECL_CONTEXT (op);
6112 if (TYPE_P (ctx) && dependent_type_p (ctx))
6113 return true;
6114 }
6115
6116 return false;
6117}
6118
6119/* The next set of functions are used for providing helpful explanatory
6120 diagnostics for failed overload resolution. Their messages should be
6121 indented by two spaces for consistency with the messages in
6122 call.c */
6123
6124static int
6125unify_success (bool /*explain_p*/)
6126{
6127 return 0;
6128}
6129
6130static int
6131unify_parameter_deduction_failure (bool explain_p, tree parm)
6132{
6133 if (explain_p)
6134 inform (input_location,
6135 " couldn't deduce template parameter %qD", parm);
6136 return 1;
6137}
6138
6139static int
6140unify_invalid (bool /*explain_p*/)
6141{
6142 return 1;
6143}
6144
6145static int
6146unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6147{
6148 if (explain_p)
6149 inform (input_location,
6150 " types %qT and %qT have incompatible cv-qualifiers",
6151 parm, arg);
6152 return 1;
6153}
6154
6155static int
6156unify_type_mismatch (bool explain_p, tree parm, tree arg)
6157{
6158 if (explain_p)
6159 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6160 return 1;
6161}
6162
6163static int
6164unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6165{
6166 if (explain_p)
6167 inform (input_location,
6168 " template parameter %qD is not a parameter pack, but "
6169 "argument %qD is",
6170 parm, arg);
6171 return 1;
6172}
6173
6174static int
6175unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6176{
6177 if (explain_p)
6178 inform (input_location,
6179 " template argument %qE does not match "
6180 "pointer-to-member constant %qE",
6181 arg, parm);
6182 return 1;
6183}
6184
6185static int
6186unify_expression_unequal (bool explain_p, tree parm, tree arg)
6187{
6188 if (explain_p)
6189 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6190 return 1;
6191}
6192
6193static int
6194unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6195{
6196 if (explain_p)
6197 inform (input_location,
6198 " inconsistent parameter pack deduction with %qT and %qT",
6199 old_arg, new_arg);
6200 return 1;
6201}
6202
6203static int
6204unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6205{
6206 if (explain_p)
6207 {
6208 if (TYPE_P (parm))
6209 inform (input_location,
6210 " deduced conflicting types for parameter %qT (%qT and %qT)",
6211 parm, first, second);
6212 else
6213 inform (input_location,
6214 " deduced conflicting values for non-type parameter "
6215 "%qE (%qE and %qE)", parm, first, second);
6216 }
6217 return 1;
6218}
6219
6220static int
6221unify_vla_arg (bool explain_p, tree arg)
6222{
6223 if (explain_p)
6224 inform (input_location,
6225 " variable-sized array type %qT is not "
6226 "a valid template argument",
6227 arg);
6228 return 1;
6229}
6230
6231static int
6232unify_method_type_error (bool explain_p, tree arg)
6233{
6234 if (explain_p)
6235 inform (input_location,
6236 " member function type %qT is not a valid template argument",
6237 arg);
6238 return 1;
6239}
6240
6241static int
6242unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6243{
6244 if (explain_p)
6245 {
6246 if (least_p)
6247 inform_n (input_location, wanted,
6248 " candidate expects at least %d argument, %d provided",
6249 " candidate expects at least %d arguments, %d provided",
6250 wanted, have);
6251 else
6252 inform_n (input_location, wanted,
6253 " candidate expects %d argument, %d provided",
6254 " candidate expects %d arguments, %d provided",
6255 wanted, have);
6256 }
6257 return 1;
6258}
6259
6260static int
6261unify_too_many_arguments (bool explain_p, int have, int wanted)
6262{
6263 return unify_arity (explain_p, have, wanted);
6264}
6265
6266static int
6267unify_too_few_arguments (bool explain_p, int have, int wanted,
6268 bool least_p = false)
6269{
6270 return unify_arity (explain_p, have, wanted, least_p);
6271}
6272
6273static int
6274unify_arg_conversion (bool explain_p, tree to_type,
6275 tree from_type, tree arg)
6276{
6277 if (explain_p)
6278 inform (EXPR_LOC_OR_LOC (arg, input_location),
6279 " cannot convert %qE (type %qT) to type %qT",
6280 arg, from_type, to_type);
6281 return 1;
6282}
6283
6284static int
6285unify_no_common_base (bool explain_p, enum template_base_result r,
6286 tree parm, tree arg)
6287{
6288 if (explain_p)
6289 switch (r)
6290 {
6291 case tbr_ambiguous_baseclass:
6292 inform (input_location, " %qT is an ambiguous base class of %qT",
6293 parm, arg);
6294 break;
6295 default:
6296 inform (input_location, " %qT is not derived from %qT", arg, parm);
6297 break;
6298 }
6299 return 1;
6300}
6301
6302static int
6303unify_inconsistent_template_template_parameters (bool explain_p)
6304{
6305 if (explain_p)
6306 inform (input_location,
6307 " template parameters of a template template argument are "
6308 "inconsistent with other deduced template arguments");
6309 return 1;
6310}
6311
6312static int
6313unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6314{
6315 if (explain_p)
6316 inform (input_location,
6317 " can't deduce a template for %qT from non-template type %qT",
6318 parm, arg);
6319 return 1;
6320}
6321
6322static int
6323unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6324{
6325 if (explain_p)
6326 inform (input_location,
6327 " template argument %qE does not match %qE", arg, parm);
6328 return 1;
6329}
6330
6331static int
6332unify_overload_resolution_failure (bool explain_p, tree arg)
6333{
6334 if (explain_p)
6335 inform (input_location,
6336 " could not resolve address from overloaded function %qE",
6337 arg);
6338 return 1;
6339}
6340
6341/* Attempt to convert the non-type template parameter EXPR to the
6342 indicated TYPE. If the conversion is successful, return the
6343 converted value. If the conversion is unsuccessful, return
6344 NULL_TREE if we issued an error message, or error_mark_node if we
6345 did not. We issue error messages for out-and-out bad template
6346 parameters, but not simply because the conversion failed, since we
6347 might be just trying to do argument deduction. Both TYPE and EXPR
6348 must be non-dependent.
6349
6350 The conversion follows the special rules described in
6351 [temp.arg.nontype], and it is much more strict than an implicit
6352 conversion.
6353
6354 This function is called twice for each template argument (see
6355 lookup_template_class for a more accurate description of this
6356 problem). This means that we need to handle expressions which
6357 are not valid in a C++ source, but can be created from the
6358 first call (for instance, casts to perform conversions). These
6359 hacks can go away after we fix the double coercion problem. */
6360
6361static tree
6362convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6363{
6364 tree expr_type;
6365
6366 /* Detect immediately string literals as invalid non-type argument.
6367 This special-case is not needed for correctness (we would easily
6368 catch this later), but only to provide better diagnostic for this
6369 common user mistake. As suggested by DR 100, we do not mention
6370 linkage issues in the diagnostic as this is not the point. */
6371 /* FIXME we're making this OK. */
6372 if (TREE_CODE (expr) == STRING_CST)
6373 {
6374 if (complain & tf_error)
6375 error ("%qE is not a valid template argument for type %qT "
6376 "because string literals can never be used in this context",
6377 expr, type);
6378 return NULL_TREE;
6379 }
6380
6381 /* Add the ADDR_EXPR now for the benefit of
6382 value_dependent_expression_p. */
6383 if (TYPE_PTROBV_P (type)
6384 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6385 {
6386 expr = decay_conversion (expr, complain);
6387 if (expr == error_mark_node)
6388 return error_mark_node;
6389 }
6390
6391 /* If we are in a template, EXPR may be non-dependent, but still
6392 have a syntactic, rather than semantic, form. For example, EXPR
6393 might be a SCOPE_REF, rather than the VAR_DECL to which the
6394 SCOPE_REF refers. Preserving the qualifying scope is necessary
6395 so that access checking can be performed when the template is
6396 instantiated -- but here we need the resolved form so that we can
6397 convert the argument. */
6398 bool non_dep = false;
6399 if (TYPE_REF_OBJ_P (type)
6400 && has_value_dependent_address (expr))
6401 /* If we want the address and it's value-dependent, don't fold. */;
6402 else if (processing_template_decl
6403 && potential_nondependent_constant_expression (expr))
6404 non_dep = true;
6405 if (error_operand_p (expr))
6406 return error_mark_node;
6407 expr_type = TREE_TYPE (expr);
6408 if (TREE_CODE (type) == REFERENCE_TYPE)
6409 expr = mark_lvalue_use (expr);
6410 else
6411 expr = mark_rvalue_use (expr);
6412
6413 /* If the argument is non-dependent, perform any conversions in
6414 non-dependent context as well. */
6415 processing_template_decl_sentinel s (non_dep);
6416 if (non_dep)
6417 expr = instantiate_non_dependent_expr_internal (expr, complain);
6418
6419 if (value_dependent_expression_p (expr))
6420 expr = canonicalize_expr_argument (expr, complain);
6421
6422 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6423 to a non-type argument of "nullptr". */
6424 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6425 expr = fold_simple (convert (type, expr));
6426
6427 /* In C++11, integral or enumeration non-type template arguments can be
6428 arbitrary constant expressions. Pointer and pointer to
6429 member arguments can be general constant expressions that evaluate
6430 to a null value, but otherwise still need to be of a specific form. */
6431 if (cxx_dialect >= cxx11)
6432 {
6433 if (TREE_CODE (expr) == PTRMEM_CST)
6434 /* A PTRMEM_CST is already constant, and a valid template
6435 argument for a parameter of pointer to member type, we just want
6436 to leave it in that form rather than lower it to a
6437 CONSTRUCTOR. */;
6438 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6439 /* Constant value checking is done later with type conversion. */;
6440 else if (cxx_dialect >= cxx1z)
6441 {
6442 if (TREE_CODE (type) != REFERENCE_TYPE)
6443 expr = maybe_constant_value (expr);
6444 else if (REFERENCE_REF_P (expr))
6445 {
6446 expr = TREE_OPERAND (expr, 0);
6447 expr = maybe_constant_value (expr);
6448 expr = convert_from_reference (expr);
6449 }
6450 }
6451 else if (TYPE_PTR_OR_PTRMEM_P (type))
6452 {
6453 tree folded = maybe_constant_value (expr);
6454 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6455 : null_member_pointer_value_p (folded))
6456 expr = folded;
6457 }
6458 }
6459
6460 /* HACK: Due to double coercion, we can get a
6461 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6462 which is the tree that we built on the first call (see
6463 below when coercing to reference to object or to reference to
6464 function). We just strip everything and get to the arg.
6465 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6466 for examples. */
6467 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6468 {
6469 tree probe_type, probe = expr;
6470 if (REFERENCE_REF_P (probe))
6471 probe = TREE_OPERAND (probe, 0);
6472 probe_type = TREE_TYPE (probe);
6473 if (TREE_CODE (probe) == NOP_EXPR)
6474 {
6475 /* ??? Maybe we could use convert_from_reference here, but we
6476 would need to relax its constraints because the NOP_EXPR
6477 could actually change the type to something more cv-qualified,
6478 and this is not folded by convert_from_reference. */
6479 tree addr = TREE_OPERAND (probe, 0);
6480 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6481 && TREE_CODE (addr) == ADDR_EXPR
6482 && TYPE_PTR_P (TREE_TYPE (addr))
6483 && (same_type_ignoring_top_level_qualifiers_p
6484 (TREE_TYPE (probe_type),
6485 TREE_TYPE (TREE_TYPE (addr)))))
6486 {
6487 expr = TREE_OPERAND (addr, 0);
6488 expr_type = TREE_TYPE (probe_type);
6489 }
6490 }
6491 }
6492
6493 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6494 parameter is a pointer to object, through decay and
6495 qualification conversion. Let's strip everything. */
6496 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6497 {
6498 tree probe = expr;
6499 STRIP_NOPS (probe);
6500 if (TREE_CODE (probe) == ADDR_EXPR
6501 && TYPE_PTR_P (TREE_TYPE (probe)))
6502 {
6503 /* Skip the ADDR_EXPR only if it is part of the decay for
6504 an array. Otherwise, it is part of the original argument
6505 in the source code. */
6506 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6507 probe = TREE_OPERAND (probe, 0);
6508 expr = probe;
6509 expr_type = TREE_TYPE (expr);
6510 }
6511 }
6512
6513 /* [temp.arg.nontype]/5, bullet 1
6514
6515 For a non-type template-parameter of integral or enumeration type,
6516 integral promotions (_conv.prom_) and integral conversions
6517 (_conv.integral_) are applied. */
6518 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6519 {
6520 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6521 t = maybe_constant_value (t);
6522 if (t != error_mark_node)
6523 expr = t;
6524
6525 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6526 return error_mark_node;
6527
6528 /* Notice that there are constant expressions like '4 % 0' which
6529 do not fold into integer constants. */
6530 if (TREE_CODE (expr) != INTEGER_CST
6531 && !value_dependent_expression_p (expr))
6532 {
6533 if (complain & tf_error)
6534 {
6535 int errs = errorcount, warns = warningcount + werrorcount;
6536 if (processing_template_decl
6537 && !require_potential_constant_expression (expr))
6538 return NULL_TREE;
6539 expr = cxx_constant_value (expr);
6540 if (errorcount > errs || warningcount + werrorcount > warns)
6541 inform (EXPR_LOC_OR_LOC (expr, input_location),
6542 "in template argument for type %qT ", type);
6543 if (expr == error_mark_node)
6544 return NULL_TREE;
6545 /* else cxx_constant_value complained but gave us
6546 a real constant, so go ahead. */
6547 if (TREE_CODE (expr) != INTEGER_CST)
6548 {
6549 /* Some assemble time constant expressions like
6550 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6551 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6552 as we can emit them into .rodata initializers of
6553 variables, yet they can't fold into an INTEGER_CST at
6554 compile time. Refuse them here. */
6555 gcc_checking_assert (reduced_constant_expression_p (expr));
6556 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6557 error_at (loc, "template argument %qE for type %qT not "
6558 "a constant integer", expr, type);
6559 return NULL_TREE;
6560 }
6561 }
6562 else
6563 return NULL_TREE;
6564 }
6565
6566 /* Avoid typedef problems. */
6567 if (TREE_TYPE (expr) != type)
6568 expr = fold_convert (type, expr);
6569 }
6570 /* [temp.arg.nontype]/5, bullet 2
6571
6572 For a non-type template-parameter of type pointer to object,
6573 qualification conversions (_conv.qual_) and the array-to-pointer
6574 conversion (_conv.array_) are applied. */
6575 else if (TYPE_PTROBV_P (type))
6576 {
6577 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6578
6579 A template-argument for a non-type, non-template template-parameter
6580 shall be one of: [...]
6581
6582 -- the name of a non-type template-parameter;
6583 -- the address of an object or function with external linkage, [...]
6584 expressed as "& id-expression" where the & is optional if the name
6585 refers to a function or array, or if the corresponding
6586 template-parameter is a reference.
6587
6588 Here, we do not care about functions, as they are invalid anyway
6589 for a parameter of type pointer-to-object. */
6590
6591 if (value_dependent_expression_p (expr))
6592 /* Non-type template parameters are OK. */
6593 ;
6594 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6595 /* Null pointer values are OK in C++11. */;
6596 else if (TREE_CODE (expr) != ADDR_EXPR
6597 && TREE_CODE (expr_type) != ARRAY_TYPE)
6598 {
6599 if (VAR_P (expr))
6600 {
6601 if (complain & tf_error)
6602 error ("%qD is not a valid template argument "
6603 "because %qD is a variable, not the address of "
6604 "a variable", expr, expr);
6605 return NULL_TREE;
6606 }
6607 if (POINTER_TYPE_P (expr_type))
6608 {
6609 if (complain & tf_error)
6610 error ("%qE is not a valid template argument for %qT "
6611 "because it is not the address of a variable",
6612 expr, type);
6613 return NULL_TREE;
6614 }
6615 /* Other values, like integer constants, might be valid
6616 non-type arguments of some other type. */
6617 return error_mark_node;
6618 }
6619 else
6620 {
6621 tree decl;
6622
6623 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6624 ? TREE_OPERAND (expr, 0) : expr);
6625 if (!VAR_P (decl))
6626 {
6627 if (complain & tf_error)
6628 error ("%qE is not a valid template argument of type %qT "
6629 "because %qE is not a variable", expr, type, decl);
6630 return NULL_TREE;
6631 }
6632 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6633 {
6634 if (complain & tf_error)
6635 error ("%qE is not a valid template argument of type %qT "
6636 "because %qD does not have external linkage",
6637 expr, type, decl);
6638 return NULL_TREE;
6639 }
6640 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6641 && decl_linkage (decl) == lk_none)
6642 {
6643 if (complain & tf_error)
6644 error ("%qE is not a valid template argument of type %qT "
6645 "because %qD has no linkage", expr, type, decl);
6646 return NULL_TREE;
6647 }
6648 /* C++17: For a non-type template-parameter of reference or pointer
6649 type, the value of the constant expression shall not refer to (or
6650 for a pointer type, shall not be the address of):
6651 * a subobject (4.5),
6652 * a temporary object (15.2),
6653 * a string literal (5.13.5),
6654 * the result of a typeid expression (8.2.8), or
6655 * a predefined __func__ variable (11.4.1). */
6656 else if (DECL_ARTIFICIAL (decl))
6657 {
6658 if (complain & tf_error)
6659 error ("the address of %qD is not a valid template argument",
6660 decl);
6661 return NULL_TREE;
6662 }
6663 else if (!same_type_ignoring_top_level_qualifiers_p
6664 (strip_array_types (TREE_TYPE (type)),
6665 strip_array_types (TREE_TYPE (decl))))
6666 {
6667 if (complain & tf_error)
6668 error ("the address of the %qT subobject of %qD is not a "
6669 "valid template argument", TREE_TYPE (type), decl);
6670 return NULL_TREE;
6671 }
6672 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6673 {
6674 if (complain & tf_error)
6675 error ("the address of %qD is not a valid template argument "
6676 "because it does not have static storage duration",
6677 decl);
6678 return NULL_TREE;
6679 }
6680 }
6681
6682 expr = decay_conversion (expr, complain);
6683 if (expr == error_mark_node)
6684 return error_mark_node;
6685
6686 expr = perform_qualification_conversions (type, expr);
6687 if (expr == error_mark_node)
6688 return error_mark_node;
6689 }
6690 /* [temp.arg.nontype]/5, bullet 3
6691
6692 For a non-type template-parameter of type reference to object, no
6693 conversions apply. The type referred to by the reference may be more
6694 cv-qualified than the (otherwise identical) type of the
6695 template-argument. The template-parameter is bound directly to the
6696 template-argument, which must be an lvalue. */
6697 else if (TYPE_REF_OBJ_P (type))
6698 {
6699 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6700 expr_type))
6701 return error_mark_node;
6702
6703 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6704 {
6705 if (complain & tf_error)
6706 error ("%qE is not a valid template argument for type %qT "
6707 "because of conflicts in cv-qualification", expr, type);
6708 return NULL_TREE;
6709 }
6710
6711 if (!lvalue_p (expr))
6712 {
6713 if (complain & tf_error)
6714 error ("%qE is not a valid template argument for type %qT "
6715 "because it is not an lvalue", expr, type);
6716 return NULL_TREE;
6717 }
6718
6719 /* [temp.arg.nontype]/1
6720
6721 A template-argument for a non-type, non-template template-parameter
6722 shall be one of: [...]
6723
6724 -- the address of an object or function with external linkage. */
6725 if (INDIRECT_REF_P (expr)
6726 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6727 {
6728 expr = TREE_OPERAND (expr, 0);
6729 if (DECL_P (expr))
6730 {
6731 if (complain & tf_error)
6732 error ("%q#D is not a valid template argument for type %qT "
6733 "because a reference variable does not have a constant "
6734 "address", expr, type);
6735 return NULL_TREE;
6736 }
6737 }
6738
6739 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6740 && value_dependent_expression_p (expr))
6741 /* OK, dependent reference. We don't want to ask whether a DECL is
6742 itself value-dependent, since what we want here is its address. */;
6743 else
6744 {
6745 if (!DECL_P (expr))
6746 {
6747 if (complain & tf_error)
6748 error ("%qE is not a valid template argument for type %qT "
6749 "because it is not an object with linkage",
6750 expr, type);
6751 return NULL_TREE;
6752 }
6753
6754 /* DR 1155 allows internal linkage in C++11 and up. */
6755 linkage_kind linkage = decl_linkage (expr);
6756 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6757 {
6758 if (complain & tf_error)
6759 error ("%qE is not a valid template argument for type %qT "
6760 "because object %qD does not have linkage",
6761 expr, type, expr);
6762 return NULL_TREE;
6763 }
6764
6765 expr = build_address (expr);
6766 }
6767
6768 if (!same_type_p (type, TREE_TYPE (expr)))
6769 expr = build_nop (type, expr);
6770 }
6771 /* [temp.arg.nontype]/5, bullet 4
6772
6773 For a non-type template-parameter of type pointer to function, only
6774 the function-to-pointer conversion (_conv.func_) is applied. If the
6775 template-argument represents a set of overloaded functions (or a
6776 pointer to such), the matching function is selected from the set
6777 (_over.over_). */
6778 else if (TYPE_PTRFN_P (type))
6779 {
6780 /* If the argument is a template-id, we might not have enough
6781 context information to decay the pointer. */
6782 if (!type_unknown_p (expr_type))
6783 {
6784 expr = decay_conversion (expr, complain);
6785 if (expr == error_mark_node)
6786 return error_mark_node;
6787 }
6788
6789 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6790 /* Null pointer values are OK in C++11. */
6791 return perform_qualification_conversions (type, expr);
6792
6793 expr = convert_nontype_argument_function (type, expr, complain);
6794 if (!expr || expr == error_mark_node)
6795 return expr;
6796 }
6797 /* [temp.arg.nontype]/5, bullet 5
6798
6799 For a non-type template-parameter of type reference to function, no
6800 conversions apply. If the template-argument represents a set of
6801 overloaded functions, the matching function is selected from the set
6802 (_over.over_). */
6803 else if (TYPE_REFFN_P (type))
6804 {
6805 if (TREE_CODE (expr) == ADDR_EXPR)
6806 {
6807 if (complain & tf_error)
6808 {
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because it is a pointer", expr, type);
6811 inform (input_location, "try using %qE instead",
6812 TREE_OPERAND (expr, 0));
6813 }
6814 return NULL_TREE;
6815 }
6816
6817 expr = convert_nontype_argument_function (type, expr, complain);
6818 if (!expr || expr == error_mark_node)
6819 return expr;
6820 }
6821 /* [temp.arg.nontype]/5, bullet 6
6822
6823 For a non-type template-parameter of type pointer to member function,
6824 no conversions apply. If the template-argument represents a set of
6825 overloaded member functions, the matching member function is selected
6826 from the set (_over.over_). */
6827 else if (TYPE_PTRMEMFUNC_P (type))
6828 {
6829 expr = instantiate_type (type, expr, tf_none);
6830 if (expr == error_mark_node)
6831 return error_mark_node;
6832
6833 /* [temp.arg.nontype] bullet 1 says the pointer to member
6834 expression must be a pointer-to-member constant. */
6835 if (!value_dependent_expression_p (expr)
6836 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6837 return error_mark_node;
6838
6839 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6840 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6841 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6842 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6843
6844 /* There is no way to disable standard conversions in
6845 resolve_address_of_overloaded_function (called by
6846 instantiate_type). It is possible that the call succeeded by
6847 converting &B::I to &D::I (where B is a base of D), so we need
6848 to reject this conversion here.
6849
6850 Actually, even if there was a way to disable standard conversions,
6851 it would still be better to reject them here so that we can
6852 provide a superior diagnostic. */
6853 if (!same_type_p (TREE_TYPE (expr), type))
6854 {
6855 if (complain & tf_error)
6856 {
6857 error ("%qE is not a valid template argument for type %qT "
6858 "because it is of type %qT", expr, type,
6859 TREE_TYPE (expr));
6860 /* If we are just one standard conversion off, explain. */
6861 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6862 inform (input_location,
6863 "standard conversions are not allowed in this context");
6864 }
6865 return NULL_TREE;
6866 }
6867 }
6868 /* [temp.arg.nontype]/5, bullet 7
6869
6870 For a non-type template-parameter of type pointer to data member,
6871 qualification conversions (_conv.qual_) are applied. */
6872 else if (TYPE_PTRDATAMEM_P (type))
6873 {
6874 /* [temp.arg.nontype] bullet 1 says the pointer to member
6875 expression must be a pointer-to-member constant. */
6876 if (!value_dependent_expression_p (expr)
6877 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6878 return error_mark_node;
6879
6880 expr = perform_qualification_conversions (type, expr);
6881 if (expr == error_mark_node)
6882 return expr;
6883 }
6884 else if (NULLPTR_TYPE_P (type))
6885 {
6886 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6887 {
6888 if (complain & tf_error)
6889 error ("%qE is not a valid template argument for type %qT "
6890 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6891 return NULL_TREE;
6892 }
6893 return expr;
6894 }
6895 /* A template non-type parameter must be one of the above. */
6896 else
6897 gcc_unreachable ();
6898
6899 /* Sanity check: did we actually convert the argument to the
6900 right type? */
6901 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6902 (type, TREE_TYPE (expr)));
6903 return convert_from_reference (expr);
6904}
6905
6906/* Subroutine of coerce_template_template_parms, which returns 1 if
6907 PARM_PARM and ARG_PARM match using the rule for the template
6908 parameters of template template parameters. Both PARM and ARG are
6909 template parameters; the rest of the arguments are the same as for
6910 coerce_template_template_parms.
6911 */
6912static int
6913coerce_template_template_parm (tree parm,
6914 tree arg,
6915 tsubst_flags_t complain,
6916 tree in_decl,
6917 tree outer_args)
6918{
6919 if (arg == NULL_TREE || error_operand_p (arg)
6920 || parm == NULL_TREE || error_operand_p (parm))
6921 return 0;
6922
6923 if (TREE_CODE (arg) != TREE_CODE (parm))
6924 return 0;
6925
6926 switch (TREE_CODE (parm))
6927 {
6928 case TEMPLATE_DECL:
6929 /* We encounter instantiations of templates like
6930 template <template <template <class> class> class TT>
6931 class C; */
6932 {
6933 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6934 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6935
6936 if (!coerce_template_template_parms
6937 (parmparm, argparm, complain, in_decl, outer_args))
6938 return 0;
6939 }
6940 /* Fall through. */
6941
6942 case TYPE_DECL:
6943 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6944 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6945 /* Argument is a parameter pack but parameter is not. */
6946 return 0;
6947 break;
6948
6949 case PARM_DECL:
6950 /* The tsubst call is used to handle cases such as
6951
6952 template <int> class C {};
6953 template <class T, template <T> class TT> class D {};
6954 D<int, C> d;
6955
6956 i.e. the parameter list of TT depends on earlier parameters. */
6957 if (!uses_template_parms (TREE_TYPE (arg)))
6958 {
6959 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6960 if (!uses_template_parms (t)
6961 && !same_type_p (t, TREE_TYPE (arg)))
6962 return 0;
6963 }
6964
6965 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6966 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6967 /* Argument is a parameter pack but parameter is not. */
6968 return 0;
6969
6970 break;
6971
6972 default:
6973 gcc_unreachable ();
6974 }
6975
6976 return 1;
6977}
6978
6979/* Coerce template argument list ARGLIST for use with template
6980 template-parameter TEMPL. */
6981
6982static tree
6983coerce_template_args_for_ttp (tree templ, tree arglist,
6984 tsubst_flags_t complain)
6985{
6986 /* Consider an example where a template template parameter declared as
6987
6988 template <class T, class U = std::allocator<T> > class TT
6989
6990 The template parameter level of T and U are one level larger than
6991 of TT. To proper process the default argument of U, say when an
6992 instantiation `TT<int>' is seen, we need to build the full
6993 arguments containing {int} as the innermost level. Outer levels,
6994 available when not appearing as default template argument, can be
6995 obtained from the arguments of the enclosing template.
6996
6997 Suppose that TT is later substituted with std::vector. The above
6998 instantiation is `TT<int, std::allocator<T> >' with TT at
6999 level 1, and T at level 2, while the template arguments at level 1
7000 becomes {std::vector} and the inner level 2 is {int}. */
7001
7002 tree outer = DECL_CONTEXT (templ);
7003 if (outer)
7004 {
7005 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7006 /* We want arguments for the partial specialization, not arguments for
7007 the primary template. */
7008 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7009 else
7010 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7011 }
7012 else if (current_template_parms)
7013 {
7014 /* This is an argument of the current template, so we haven't set
7015 DECL_CONTEXT yet. */
7016 tree relevant_template_parms;
7017
7018 /* Parameter levels that are greater than the level of the given
7019 template template parm are irrelevant. */
7020 relevant_template_parms = current_template_parms;
7021 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7022 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7023 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7024
7025 outer = template_parms_to_args (relevant_template_parms);
7026 }
7027
7028 if (outer)
7029 arglist = add_to_template_args (outer, arglist);
7030
7031 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7032 return coerce_template_parms (parmlist, arglist, templ,
7033 complain,
7034 /*require_all_args=*/true,
7035 /*use_default_args=*/true);
7036}
7037
7038/* A cache of template template parameters with match-all default
7039 arguments. */
7040static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7041static void
7042store_defaulted_ttp (tree v, tree t)
7043{
7044 if (!defaulted_ttp_cache)
7045 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7046 defaulted_ttp_cache->put (v, t);
7047}
7048static tree
7049lookup_defaulted_ttp (tree v)
7050{
7051 if (defaulted_ttp_cache)
7052 if (tree *p = defaulted_ttp_cache->get (v))
7053 return *p;
7054 return NULL_TREE;
7055}
7056
7057/* T is a bound template template-parameter. Copy its arguments into default
7058 arguments of the template template-parameter's template parameters. */
7059
7060static tree
7061add_defaults_to_ttp (tree otmpl)
7062{
7063 if (tree c = lookup_defaulted_ttp (otmpl))
7064 return c;
7065
7066 tree ntmpl = copy_node (otmpl);
7067
7068 tree ntype = copy_node (TREE_TYPE (otmpl));
7069 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7070 TYPE_MAIN_VARIANT (ntype) = ntype;
7071 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7072 TYPE_NAME (ntype) = ntmpl;
7073 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7074
7075 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7076 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7077 TEMPLATE_PARM_DECL (idx) = ntmpl;
7078 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7079
7080 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7081 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7082 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7083 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7084 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7085 {
7086 tree o = TREE_VEC_ELT (vec, i);
7087 if (!template_parameter_pack_p (TREE_VALUE (o)))
7088 {
7089 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7090 TREE_PURPOSE (n) = any_targ_node;
7091 }
7092 }
7093
7094 store_defaulted_ttp (otmpl, ntmpl);
7095 return ntmpl;
7096}
7097
7098/* ARG is a bound potential template template-argument, and PARGS is a list
7099 of arguments for the corresponding template template-parameter. Adjust
7100 PARGS as appropriate for application to ARG's template, and if ARG is a
7101 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7102 arguments to the template template parameter. */
7103
7104static tree
7105coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7106{
7107 ++processing_template_decl;
7108 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7110 {
7111 /* When comparing two template template-parameters in partial ordering,
7112 rewrite the one currently being used as an argument to have default
7113 arguments for all parameters. */
7114 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7115 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7116 if (pargs != error_mark_node)
7117 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7118 TYPE_TI_ARGS (arg));
7119 }
7120 else
7121 {
7122 tree aparms
7123 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7124 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7125 /*require_all*/true,
7126 /*use_default*/true);
7127 }
7128 --processing_template_decl;
7129 return pargs;
7130}
7131
7132/* Subroutine of unify for the case when PARM is a
7133 BOUND_TEMPLATE_TEMPLATE_PARM. */
7134
7135static int
7136unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7137 bool explain_p)
7138{
7139 tree parmvec = TYPE_TI_ARGS (parm);
7140 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7141
7142 /* The template template parm might be variadic and the argument
7143 not, so flatten both argument lists. */
7144 parmvec = expand_template_argument_pack (parmvec);
7145 argvec = expand_template_argument_pack (argvec);
7146
7147 if (flag_new_ttp)
7148 {
7149 /* In keeping with P0522R0, adjust P's template arguments
7150 to apply to A's template; then flatten it again. */
7151 tree nparmvec = parmvec;
7152 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7153 nparmvec = expand_template_argument_pack (nparmvec);
7154
7155 if (unify (tparms, targs, nparmvec, argvec,
7156 UNIFY_ALLOW_NONE, explain_p))
7157 return 1;
7158
7159 /* If the P0522 adjustment eliminated a pack expansion, deduce
7160 empty packs. */
7161 if (flag_new_ttp
7162 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7163 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7164 DEDUCE_EXACT, /*sub*/true, explain_p))
7165 return 1;
7166 }
7167 else
7168 {
7169 /* Deduce arguments T, i from TT<T> or TT<i>.
7170 We check each element of PARMVEC and ARGVEC individually
7171 rather than the whole TREE_VEC since they can have
7172 different number of elements, which is allowed under N2555. */
7173
7174 int len = TREE_VEC_LENGTH (parmvec);
7175
7176 /* Check if the parameters end in a pack, making them
7177 variadic. */
7178 int parm_variadic_p = 0;
7179 if (len > 0
7180 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7181 parm_variadic_p = 1;
7182
7183 for (int i = 0; i < len - parm_variadic_p; ++i)
7184 /* If the template argument list of P contains a pack
7185 expansion that is not the last template argument, the
7186 entire template argument list is a non-deduced
7187 context. */
7188 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7189 return unify_success (explain_p);
7190
7191 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7192 return unify_too_few_arguments (explain_p,
7193 TREE_VEC_LENGTH (argvec), len);
7194
7195 for (int i = 0; i < len - parm_variadic_p; ++i)
7196 if (unify (tparms, targs,
7197 TREE_VEC_ELT (parmvec, i),
7198 TREE_VEC_ELT (argvec, i),
7199 UNIFY_ALLOW_NONE, explain_p))
7200 return 1;
7201
7202 if (parm_variadic_p
7203 && unify_pack_expansion (tparms, targs,
7204 parmvec, argvec,
7205 DEDUCE_EXACT,
7206 /*subr=*/true, explain_p))
7207 return 1;
7208 }
7209
7210 return 0;
7211}
7212
7213/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7214 template template parameters. Both PARM_PARMS and ARG_PARMS are
7215 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7216 or PARM_DECL.
7217
7218 Consider the example:
7219 template <class T> class A;
7220 template<template <class U> class TT> class B;
7221
7222 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7223 the parameters to A, and OUTER_ARGS contains A. */
7224
7225static int
7226coerce_template_template_parms (tree parm_parms,
7227 tree arg_parms,
7228 tsubst_flags_t complain,
7229 tree in_decl,
7230 tree outer_args)
7231{
7232 int nparms, nargs, i;
7233 tree parm, arg;
7234 int variadic_p = 0;
7235
7236 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7237 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7238
7239 nparms = TREE_VEC_LENGTH (parm_parms);
7240 nargs = TREE_VEC_LENGTH (arg_parms);
7241
7242 if (flag_new_ttp)
7243 {
7244 /* P0522R0: A template template-parameter P is at least as specialized as
7245 a template template-argument A if, given the following rewrite to two
7246 function templates, the function template corresponding to P is at
7247 least as specialized as the function template corresponding to A
7248 according to the partial ordering rules for function templates
7249 ([temp.func.order]). Given an invented class template X with the
7250 template parameter list of A (including default arguments):
7251
7252 * Each of the two function templates has the same template parameters,
7253 respectively, as P or A.
7254
7255 * Each function template has a single function parameter whose type is
7256 a specialization of X with template arguments corresponding to the
7257 template parameters from the respective function template where, for
7258 each template parameter PP in the template parameter list of the
7259 function template, a corresponding template argument AA is formed. If
7260 PP declares a parameter pack, then AA is the pack expansion
7261 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7262
7263 If the rewrite produces an invalid type, then P is not at least as
7264 specialized as A. */
7265
7266 /* So coerce P's args to apply to A's parms, and then deduce between A's
7267 args and the converted args. If that succeeds, A is at least as
7268 specialized as P, so they match.*/
7269 tree pargs = template_parms_level_to_args (parm_parms);
7270 ++processing_template_decl;
7271 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7272 /*require_all*/true, /*use_default*/true);
7273 --processing_template_decl;
7274 if (pargs != error_mark_node)
7275 {
7276 tree targs = make_tree_vec (nargs);
7277 tree aargs = template_parms_level_to_args (arg_parms);
7278 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7279 /*explain*/false))
7280 return 1;
7281 }
7282 }
7283
7284 /* Determine whether we have a parameter pack at the end of the
7285 template template parameter's template parameter list. */
7286 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7287 {
7288 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7289
7290 if (error_operand_p (parm))
7291 return 0;
7292
7293 switch (TREE_CODE (parm))
7294 {
7295 case TEMPLATE_DECL:
7296 case TYPE_DECL:
7297 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7298 variadic_p = 1;
7299 break;
7300
7301 case PARM_DECL:
7302 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7303 variadic_p = 1;
7304 break;
7305
7306 default:
7307 gcc_unreachable ();
7308 }
7309 }
7310
7311 if (nargs != nparms
7312 && !(variadic_p && nargs >= nparms - 1))
7313 return 0;
7314
7315 /* Check all of the template parameters except the parameter pack at
7316 the end (if any). */
7317 for (i = 0; i < nparms - variadic_p; ++i)
7318 {
7319 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7320 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7321 continue;
7322
7323 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7324 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7325
7326 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7327 outer_args))
7328 return 0;
7329
7330 }
7331
7332 if (variadic_p)
7333 {
7334 /* Check each of the template parameters in the template
7335 argument against the template parameter pack at the end of
7336 the template template parameter. */
7337 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7338 return 0;
7339
7340 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7341
7342 for (; i < nargs; ++i)
7343 {
7344 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7345 continue;
7346
7347 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7348
7349 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7350 outer_args))
7351 return 0;
7352 }
7353 }
7354
7355 return 1;
7356}
7357
7358/* Verifies that the deduced template arguments (in TARGS) for the
7359 template template parameters (in TPARMS) represent valid bindings,
7360 by comparing the template parameter list of each template argument
7361 to the template parameter list of its corresponding template
7362 template parameter, in accordance with DR150. This
7363 routine can only be called after all template arguments have been
7364 deduced. It will return TRUE if all of the template template
7365 parameter bindings are okay, FALSE otherwise. */
7366bool
7367template_template_parm_bindings_ok_p (tree tparms, tree targs)
7368{
7369 int i, ntparms = TREE_VEC_LENGTH (tparms);
7370 bool ret = true;
7371
7372 /* We're dealing with template parms in this process. */
7373 ++processing_template_decl;
7374
7375 targs = INNERMOST_TEMPLATE_ARGS (targs);
7376
7377 for (i = 0; i < ntparms; ++i)
7378 {
7379 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7380 tree targ = TREE_VEC_ELT (targs, i);
7381
7382 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7383 {
7384 tree packed_args = NULL_TREE;
7385 int idx, len = 1;
7386
7387 if (ARGUMENT_PACK_P (targ))
7388 {
7389 /* Look inside the argument pack. */
7390 packed_args = ARGUMENT_PACK_ARGS (targ);
7391 len = TREE_VEC_LENGTH (packed_args);
7392 }
7393
7394 for (idx = 0; idx < len; ++idx)
7395 {
7396 tree targ_parms = NULL_TREE;
7397
7398 if (packed_args)
7399 /* Extract the next argument from the argument
7400 pack. */
7401 targ = TREE_VEC_ELT (packed_args, idx);
7402
7403 if (PACK_EXPANSION_P (targ))
7404 /* Look at the pattern of the pack expansion. */
7405 targ = PACK_EXPANSION_PATTERN (targ);
7406
7407 /* Extract the template parameters from the template
7408 argument. */
7409 if (TREE_CODE (targ) == TEMPLATE_DECL)
7410 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7411 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7412 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7413
7414 /* Verify that we can coerce the template template
7415 parameters from the template argument to the template
7416 parameter. This requires an exact match. */
7417 if (targ_parms
7418 && !coerce_template_template_parms
7419 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7420 targ_parms,
7421 tf_none,
7422 tparm,
7423 targs))
7424 {
7425 ret = false;
7426 goto out;
7427 }
7428 }
7429 }
7430 }
7431
7432 out:
7433
7434 --processing_template_decl;
7435 return ret;
7436}
7437
7438/* Since type attributes aren't mangled, we need to strip them from
7439 template type arguments. */
7440
7441static tree
7442canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7443{
7444 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7445 return arg;
7446 bool removed_attributes = false;
7447 tree canon = strip_typedefs (arg, &removed_attributes);
7448 if (removed_attributes
7449 && (complain & tf_warning))
7450 warning (OPT_Wignored_attributes,
7451 "ignoring attributes on template argument %qT", arg);
7452 return canon;
7453}
7454
7455/* And from inside dependent non-type arguments like sizeof(Type). */
7456
7457static tree
7458canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7459{
7460 if (!arg || arg == error_mark_node)
7461 return arg;
7462 bool removed_attributes = false;
7463 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7464 if (removed_attributes
7465 && (complain & tf_warning))
7466 warning (OPT_Wignored_attributes,
7467 "ignoring attributes in template argument %qE", arg);
7468 return canon;
7469}
7470
7471// A template declaration can be substituted for a constrained
7472// template template parameter only when the argument is more
7473// constrained than the parameter.
7474static bool
7475is_compatible_template_arg (tree parm, tree arg)
7476{
7477 tree parm_cons = get_constraints (parm);
7478
7479 /* For now, allow constrained template template arguments
7480 and unconstrained template template parameters. */
7481 if (parm_cons == NULL_TREE)
7482 return true;
7483
7484 tree arg_cons = get_constraints (arg);
7485
7486 // If the template parameter is constrained, we need to rewrite its
7487 // constraints in terms of the ARG's template parameters. This ensures
7488 // that all of the template parameter types will have the same depth.
7489 //
7490 // Note that this is only valid when coerce_template_template_parm is
7491 // true for the innermost template parameters of PARM and ARG. In other
7492 // words, because coercion is successful, this conversion will be valid.
7493 if (parm_cons)
7494 {
7495 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7496 parm_cons = tsubst_constraint_info (parm_cons,
7497 INNERMOST_TEMPLATE_ARGS (args),
7498 tf_none, NULL_TREE);
7499 if (parm_cons == error_mark_node)
7500 return false;
7501 }
7502
7503 return subsumes (parm_cons, arg_cons);
7504}
7505
7506// Convert a placeholder argument into a binding to the original
7507// parameter. The original parameter is saved as the TREE_TYPE of
7508// ARG.
7509static inline tree
7510convert_wildcard_argument (tree parm, tree arg)
7511{
7512 TREE_TYPE (arg) = parm;
7513 return arg;
7514}
7515
7516/* Convert the indicated template ARG as necessary to match the
7517 indicated template PARM. Returns the converted ARG, or
7518 error_mark_node if the conversion was unsuccessful. Error and
7519 warning messages are issued under control of COMPLAIN. This
7520 conversion is for the Ith parameter in the parameter list. ARGS is
7521 the full set of template arguments deduced so far. */
7522
7523static tree
7524convert_template_argument (tree parm,
7525 tree arg,
7526 tree args,
7527 tsubst_flags_t complain,
7528 int i,
7529 tree in_decl)
7530{
7531 tree orig_arg;
7532 tree val;
7533 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7534
7535 if (parm == error_mark_node)
7536 return error_mark_node;
7537
7538 /* Trivially convert placeholders. */
7539 if (TREE_CODE (arg) == WILDCARD_DECL)
7540 return convert_wildcard_argument (parm, arg);
7541
7542 if (arg == any_targ_node)
7543 return arg;
7544
7545 if (TREE_CODE (arg) == TREE_LIST
7546 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7547 {
7548 /* The template argument was the name of some
7549 member function. That's usually
7550 invalid, but static members are OK. In any
7551 case, grab the underlying fields/functions
7552 and issue an error later if required. */
7553 orig_arg = TREE_VALUE (arg);
7554 TREE_TYPE (arg) = unknown_type_node;
7555 }
7556
7557 orig_arg = arg;
7558
7559 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7560 requires_type = (TREE_CODE (parm) == TYPE_DECL
7561 || requires_tmpl_type);
7562
7563 /* When determining whether an argument pack expansion is a template,
7564 look at the pattern. */
7565 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7566 arg = PACK_EXPANSION_PATTERN (arg);
7567
7568 /* Deal with an injected-class-name used as a template template arg. */
7569 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7570 {
7571 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7572 if (TREE_CODE (t) == TEMPLATE_DECL)
7573 {
7574 if (cxx_dialect >= cxx11)
7575 /* OK under DR 1004. */;
7576 else if (complain & tf_warning_or_error)
7577 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7578 " used as template template argument", TYPE_NAME (arg));
7579 else if (flag_pedantic_errors)
7580 t = arg;
7581
7582 arg = t;
7583 }
7584 }
7585
7586 is_tmpl_type =
7587 ((TREE_CODE (arg) == TEMPLATE_DECL
7588 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7589 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7590 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7591 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7592
7593 if (is_tmpl_type
7594 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7595 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7596 arg = TYPE_STUB_DECL (arg);
7597
7598 is_type = TYPE_P (arg) || is_tmpl_type;
7599
7600 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7601 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7602 {
7603 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7604 {
7605 if (complain & tf_error)
7606 error ("invalid use of destructor %qE as a type", orig_arg);
7607 return error_mark_node;
7608 }
7609
7610 permerror (input_location,
7611 "to refer to a type member of a template parameter, "
7612 "use %<typename %E%>", orig_arg);
7613
7614 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7615 TREE_OPERAND (arg, 1),
7616 typename_type,
7617 complain);
7618 arg = orig_arg;
7619 is_type = 1;
7620 }
7621 if (is_type != requires_type)
7622 {
7623 if (in_decl)
7624 {
7625 if (complain & tf_error)
7626 {
7627 error ("type/value mismatch at argument %d in template "
7628 "parameter list for %qD",
7629 i + 1, in_decl);
7630 if (is_type)
7631 inform (input_location,
7632 " expected a constant of type %qT, got %qT",
7633 TREE_TYPE (parm),
7634 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7635 else if (requires_tmpl_type)
7636 inform (input_location,
7637 " expected a class template, got %qE", orig_arg);
7638 else
7639 inform (input_location,
7640 " expected a type, got %qE", orig_arg);
7641 }
7642 }
7643 return error_mark_node;
7644 }
7645 if (is_tmpl_type ^ requires_tmpl_type)
7646 {
7647 if (in_decl && (complain & tf_error))
7648 {
7649 error ("type/value mismatch at argument %d in template "
7650 "parameter list for %qD",
7651 i + 1, in_decl);
7652 if (is_tmpl_type)
7653 inform (input_location,
7654 " expected a type, got %qT", DECL_NAME (arg));
7655 else
7656 inform (input_location,
7657 " expected a class template, got %qT", orig_arg);
7658 }
7659 return error_mark_node;
7660 }
7661
7662 if (is_type)
7663 {
7664 if (requires_tmpl_type)
7665 {
7666 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7667 val = orig_arg;
7668 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7669 /* The number of argument required is not known yet.
7670 Just accept it for now. */
7671 val = orig_arg;
7672 else
7673 {
7674 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7675 tree argparm;
7676
7677 /* Strip alias templates that are equivalent to another
7678 template. */
7679 arg = get_underlying_template (arg);
7680 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7681
7682 if (coerce_template_template_parms (parmparm, argparm,
7683 complain, in_decl,
7684 args))
7685 {
7686 val = arg;
7687
7688 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7689 TEMPLATE_DECL. */
7690 if (val != error_mark_node)
7691 {
7692 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7693 val = TREE_TYPE (val);
7694 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7695 val = make_pack_expansion (val);
7696 }
7697 }
7698 else
7699 {
7700 if (in_decl && (complain & tf_error))
7701 {
7702 error ("type/value mismatch at argument %d in "
7703 "template parameter list for %qD",
7704 i + 1, in_decl);
7705 inform (input_location,
7706 " expected a template of type %qD, got %qT",
7707 parm, orig_arg);
7708 }
7709
7710 val = error_mark_node;
7711 }
7712
7713 // Check that the constraints are compatible before allowing the
7714 // substitution.
7715 if (val != error_mark_node)
7716 if (!is_compatible_template_arg (parm, arg))
7717 {
7718 if (in_decl && (complain & tf_error))
7719 {
7720 error ("constraint mismatch at argument %d in "
7721 "template parameter list for %qD",
7722 i + 1, in_decl);
7723 inform (input_location, " expected %qD but got %qD",
7724 parm, arg);
7725 }
7726 val = error_mark_node;
7727 }
7728 }
7729 }
7730 else
7731 val = orig_arg;
7732 /* We only form one instance of each template specialization.
7733 Therefore, if we use a non-canonical variant (i.e., a
7734 typedef), any future messages referring to the type will use
7735 the typedef, which is confusing if those future uses do not
7736 themselves also use the typedef. */
7737 if (TYPE_P (val))
7738 val = canonicalize_type_argument (val, complain);
7739 }
7740 else
7741 {
7742 tree t = TREE_TYPE (parm);
7743
7744 if (tree a = type_uses_auto (t))
7745 {
7746 if (ARGUMENT_PACK_P (orig_arg))
7747 /* There's nothing to check for an auto argument pack. */
7748 return orig_arg;
7749
7750 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7751 if (t == error_mark_node)
7752 return error_mark_node;
7753 }
7754 else
7755 t = tsubst (t, args, complain, in_decl);
7756
7757 if (invalid_nontype_parm_type_p (t, complain))
7758 return error_mark_node;
7759
7760 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7761 {
7762 if (same_type_p (t, TREE_TYPE (orig_arg)))
7763 val = orig_arg;
7764 else
7765 {
7766 /* Not sure if this is reachable, but it doesn't hurt
7767 to be robust. */
7768 error ("type mismatch in nontype parameter pack");
7769 val = error_mark_node;
7770 }
7771 }
7772 else if (!type_dependent_expression_p (orig_arg)
7773 && !uses_template_parms (t))
7774 /* We used to call digest_init here. However, digest_init
7775 will report errors, which we don't want when complain
7776 is zero. More importantly, digest_init will try too
7777 hard to convert things: for example, `0' should not be
7778 converted to pointer type at this point according to
7779 the standard. Accepting this is not merely an
7780 extension, since deciding whether or not these
7781 conversions can occur is part of determining which
7782 function template to call, or whether a given explicit
7783 argument specification is valid. */
7784 val = convert_nontype_argument (t, orig_arg, complain);
7785 else
7786 val = canonicalize_expr_argument (orig_arg, complain);
7787
7788 if (val == NULL_TREE)
7789 val = error_mark_node;
7790 else if (val == error_mark_node && (complain & tf_error))
7791 error ("could not convert template argument %qE from %qT to %qT",
7792 orig_arg, TREE_TYPE (orig_arg), t);
7793
7794 if (INDIRECT_REF_P (val))
7795 {
7796 /* Reject template arguments that are references to built-in
7797 functions with no library fallbacks. */
7798 const_tree inner = TREE_OPERAND (val, 0);
7799 const_tree innertype = TREE_TYPE (inner);
7800 if (innertype
7801 && TREE_CODE (innertype) == REFERENCE_TYPE
7802 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7803 && 0 < TREE_OPERAND_LENGTH (inner)
7804 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7805 return error_mark_node;
7806 }
7807
7808 if (TREE_CODE (val) == SCOPE_REF)
7809 {
7810 /* Strip typedefs from the SCOPE_REF. */
7811 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7812 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7813 complain);
7814 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7815 QUALIFIED_NAME_IS_TEMPLATE (val));
7816 }
7817 }
7818
7819 return val;
7820}
7821
7822/* Coerces the remaining template arguments in INNER_ARGS (from
7823 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7824 Returns the coerced argument pack. PARM_IDX is the position of this
7825 parameter in the template parameter list. ARGS is the original
7826 template argument list. */
7827static tree
7828coerce_template_parameter_pack (tree parms,
7829 int parm_idx,
7830 tree args,
7831 tree inner_args,
7832 int arg_idx,
7833 tree new_args,
7834 int* lost,
7835 tree in_decl,
7836 tsubst_flags_t complain)
7837{
7838 tree parm = TREE_VEC_ELT (parms, parm_idx);
7839 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7840 tree packed_args;
7841 tree argument_pack;
7842 tree packed_parms = NULL_TREE;
7843
7844 if (arg_idx > nargs)
7845 arg_idx = nargs;
7846
7847 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7848 {
7849 /* When the template parameter is a non-type template parameter pack
7850 or template template parameter pack whose type or template
7851 parameters use parameter packs, we know exactly how many arguments
7852 we are looking for. Build a vector of the instantiated decls for
7853 these template parameters in PACKED_PARMS. */
7854 /* We can't use make_pack_expansion here because it would interpret a
7855 _DECL as a use rather than a declaration. */
7856 tree decl = TREE_VALUE (parm);
7857 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7858 SET_PACK_EXPANSION_PATTERN (exp, decl);
7859 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7860 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7861
7862 TREE_VEC_LENGTH (args)--;
7863 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7864 TREE_VEC_LENGTH (args)++;
7865
7866 if (packed_parms == error_mark_node)
7867 return error_mark_node;
7868
7869 /* If we're doing a partial instantiation of a member template,
7870 verify that all of the types used for the non-type
7871 template parameter pack are, in fact, valid for non-type
7872 template parameters. */
7873 if (arg_idx < nargs
7874 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7875 {
7876 int j, len = TREE_VEC_LENGTH (packed_parms);
7877 for (j = 0; j < len; ++j)
7878 {
7879 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7880 if (invalid_nontype_parm_type_p (t, complain))
7881 return error_mark_node;
7882 }
7883 /* We don't know how many args we have yet, just
7884 use the unconverted ones for now. */
7885 return NULL_TREE;
7886 }
7887
7888 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7889 }
7890 /* Check if we have a placeholder pack, which indicates we're
7891 in the context of a introduction list. In that case we want
7892 to match this pack to the single placeholder. */
7893 else if (arg_idx < nargs
7894 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7895 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7896 {
7897 nargs = arg_idx + 1;
7898 packed_args = make_tree_vec (1);
7899 }
7900 else
7901 packed_args = make_tree_vec (nargs - arg_idx);
7902
7903 /* Convert the remaining arguments, which will be a part of the
7904 parameter pack "parm". */
7905 int first_pack_arg = arg_idx;
7906 for (; arg_idx < nargs; ++arg_idx)
7907 {
7908 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7909 tree actual_parm = TREE_VALUE (parm);
7910 int pack_idx = arg_idx - first_pack_arg;
7911
7912 if (packed_parms)
7913 {
7914 /* Once we've packed as many args as we have types, stop. */
7915 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7916 break;
7917 else if (PACK_EXPANSION_P (arg))
7918 /* We don't know how many args we have yet, just
7919 use the unconverted ones for now. */
7920 return NULL_TREE;
7921 else
7922 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7923 }
7924
7925 if (arg == error_mark_node)
7926 {
7927 if (complain & tf_error)
7928 error ("template argument %d is invalid", arg_idx + 1);
7929 }
7930 else
7931 arg = convert_template_argument (actual_parm,
7932 arg, new_args, complain, parm_idx,
7933 in_decl);
7934 if (arg == error_mark_node)
7935 (*lost)++;
7936 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7937 }
7938
7939 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7940 && TREE_VEC_LENGTH (packed_args) > 0)
7941 {
7942 if (complain & tf_error)
7943 error ("wrong number of template arguments (%d, should be %d)",
7944 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7945 return error_mark_node;
7946 }
7947
7948 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7949 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7950 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7951 else
7952 {
7953 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7954 TREE_TYPE (argument_pack)
7955 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7956 TREE_CONSTANT (argument_pack) = 1;
7957 }
7958
7959 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7960 if (CHECKING_P)
7961 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7962 TREE_VEC_LENGTH (packed_args));
7963 return argument_pack;
7964}
7965
7966/* Returns the number of pack expansions in the template argument vector
7967 ARGS. */
7968
7969static int
7970pack_expansion_args_count (tree args)
7971{
7972 int i;
7973 int count = 0;
7974 if (args)
7975 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7976 {
7977 tree elt = TREE_VEC_ELT (args, i);
7978 if (elt && PACK_EXPANSION_P (elt))
7979 ++count;
7980 }
7981 return count;
7982}
7983
7984/* Convert all template arguments to their appropriate types, and
7985 return a vector containing the innermost resulting template
7986 arguments. If any error occurs, return error_mark_node. Error and
7987 warning messages are issued under control of COMPLAIN.
7988
7989 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7990 for arguments not specified in ARGS. Otherwise, if
7991 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7992 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7993 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7994 ARGS. */
7995
7996static tree
7997coerce_template_parms (tree parms,
7998 tree args,
7999 tree in_decl,
8000 tsubst_flags_t complain,
8001 bool require_all_args,
8002 bool use_default_args)
8003{
8004 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8005 tree orig_inner_args;
8006 tree inner_args;
8007 tree new_args;
8008 tree new_inner_args;
8009 int saved_unevaluated_operand;
8010 int saved_inhibit_evaluation_warnings;
8011
8012 /* When used as a boolean value, indicates whether this is a
8013 variadic template parameter list. Since it's an int, we can also
8014 subtract it from nparms to get the number of non-variadic
8015 parameters. */
8016 int variadic_p = 0;
8017 int variadic_args_p = 0;
8018 int post_variadic_parms = 0;
8019
8020 /* Likewise for parameters with default arguments. */
8021 int default_p = 0;
8022
8023 if (args == error_mark_node)
8024 return error_mark_node;
8025
8026 nparms = TREE_VEC_LENGTH (parms);
8027
8028 /* Determine if there are any parameter packs or default arguments. */
8029 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8030 {
8031 tree parm = TREE_VEC_ELT (parms, parm_idx);
8032 if (variadic_p)
8033 ++post_variadic_parms;
8034 if (template_parameter_pack_p (TREE_VALUE (parm)))
8035 ++variadic_p;
8036 if (TREE_PURPOSE (parm))
8037 ++default_p;
8038 }
8039
8040 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8041 /* If there are no parameters that follow a parameter pack, we need to
8042 expand any argument packs so that we can deduce a parameter pack from
8043 some non-packed args followed by an argument pack, as in variadic85.C.
8044 If there are such parameters, we need to leave argument packs intact
8045 so the arguments are assigned properly. This can happen when dealing
8046 with a nested class inside a partial specialization of a class
8047 template, as in variadic92.C, or when deducing a template parameter pack
8048 from a sub-declarator, as in variadic114.C. */
8049 if (!post_variadic_parms)
8050 inner_args = expand_template_argument_pack (inner_args);
8051
8052 /* Count any pack expansion args. */
8053 variadic_args_p = pack_expansion_args_count (inner_args);
8054
8055 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8056 if ((nargs - variadic_args_p > nparms && !variadic_p)
8057 || (nargs < nparms - variadic_p
8058 && require_all_args
8059 && !variadic_args_p
8060 && (!use_default_args
8061 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8062 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8063 {
8064 if (complain & tf_error)
8065 {
8066 if (variadic_p || default_p)
8067 {
8068 nparms -= variadic_p + default_p;
8069 error ("wrong number of template arguments "
8070 "(%d, should be at least %d)", nargs, nparms);
8071 }
8072 else
8073 error ("wrong number of template arguments "
8074 "(%d, should be %d)", nargs, nparms);
8075
8076 if (in_decl)
8077 inform (DECL_SOURCE_LOCATION (in_decl),
8078 "provided for %qD", in_decl);
8079 }
8080
8081 return error_mark_node;
8082 }
8083 /* We can't pass a pack expansion to a non-pack parameter of an alias
8084 template (DR 1430). */
8085 else if (in_decl
8086 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8087 || concept_template_p (in_decl))
8088 && variadic_args_p
8089 && nargs - variadic_args_p < nparms - variadic_p)
8090 {
8091 if (complain & tf_error)
8092 {
8093 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8094 {
8095 tree arg = TREE_VEC_ELT (inner_args, i);
8096 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8097
8098 if (PACK_EXPANSION_P (arg)
8099 && !template_parameter_pack_p (parm))
8100 {
8101 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8102 error_at (location_of (arg),
8103 "pack expansion argument for non-pack parameter "
8104 "%qD of alias template %qD", parm, in_decl);
8105 else
8106 error_at (location_of (arg),
8107 "pack expansion argument for non-pack parameter "
8108 "%qD of concept %qD", parm, in_decl);
8109 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8110 goto found;
8111 }
8112 }
8113 gcc_unreachable ();
8114 found:;
8115 }
8116 return error_mark_node;
8117 }
8118
8119 /* We need to evaluate the template arguments, even though this
8120 template-id may be nested within a "sizeof". */
8121 saved_unevaluated_operand = cp_unevaluated_operand;
8122 cp_unevaluated_operand = 0;
8123 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8124 c_inhibit_evaluation_warnings = 0;
8125 new_inner_args = make_tree_vec (nparms);
8126 new_args = add_outermost_template_args (args, new_inner_args);
8127 int pack_adjust = 0;
8128 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8129 {
8130 tree arg;
8131 tree parm;
8132
8133 /* Get the Ith template parameter. */
8134 parm = TREE_VEC_ELT (parms, parm_idx);
8135
8136 if (parm == error_mark_node)
8137 {
8138 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8139 continue;
8140 }
8141
8142 /* Calculate the next argument. */
8143 if (arg_idx < nargs)
8144 arg = TREE_VEC_ELT (inner_args, arg_idx);
8145 else
8146 arg = NULL_TREE;
8147
8148 if (template_parameter_pack_p (TREE_VALUE (parm))
8149 && !(arg && ARGUMENT_PACK_P (arg)))
8150 {
8151 /* Some arguments will be placed in the
8152 template parameter pack PARM. */
8153 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8154 inner_args, arg_idx,
8155 new_args, &lost,
8156 in_decl, complain);
8157
8158 if (arg == NULL_TREE)
8159 {
8160 /* We don't know how many args we have yet, just use the
8161 unconverted (and still packed) ones for now. */
8162 new_inner_args = orig_inner_args;
8163 arg_idx = nargs;
8164 break;
8165 }
8166
8167 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8168
8169 /* Store this argument. */
8170 if (arg == error_mark_node)
8171 {
8172 lost++;
8173 /* We are done with all of the arguments. */
8174 arg_idx = nargs;
8175 }
8176 else
8177 {
8178 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8179 arg_idx += pack_adjust;
8180 }
8181
8182 continue;
8183 }
8184 else if (arg)
8185 {
8186 if (PACK_EXPANSION_P (arg))
8187 {
8188 /* "If every valid specialization of a variadic template
8189 requires an empty template parameter pack, the template is
8190 ill-formed, no diagnostic required." So check that the
8191 pattern works with this parameter. */
8192 tree pattern = PACK_EXPANSION_PATTERN (arg);
8193 tree conv = convert_template_argument (TREE_VALUE (parm),
8194 pattern, new_args,
8195 complain, parm_idx,
8196 in_decl);
8197 if (conv == error_mark_node)
8198 {
8199 if (complain & tf_error)
8200 inform (input_location, "so any instantiation with a "
8201 "non-empty parameter pack would be ill-formed");
8202 ++lost;
8203 }
8204 else if (TYPE_P (conv) && !TYPE_P (pattern))
8205 /* Recover from missing typename. */
8206 TREE_VEC_ELT (inner_args, arg_idx)
8207 = make_pack_expansion (conv);
8208
8209 /* We don't know how many args we have yet, just
8210 use the unconverted ones for now. */
8211 new_inner_args = inner_args;
8212 arg_idx = nargs;
8213 break;
8214 }
8215 }
8216 else if (require_all_args)
8217 {
8218 /* There must be a default arg in this case. */
8219 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8220 complain, in_decl);
8221 /* The position of the first default template argument,
8222 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8223 Record that. */
8224 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8225 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8226 arg_idx - pack_adjust);
8227 }
8228 else
8229 break;
8230
8231 if (arg == error_mark_node)
8232 {
8233 if (complain & tf_error)
8234 error ("template argument %d is invalid", arg_idx + 1);
8235 }
8236 else if (!arg)
8237 /* This only occurs if there was an error in the template
8238 parameter list itself (which we would already have
8239 reported) that we are trying to recover from, e.g., a class
8240 template with a parameter list such as
8241 template<typename..., typename>. */
8242 ++lost;
8243 else
8244 arg = convert_template_argument (TREE_VALUE (parm),
8245 arg, new_args, complain,
8246 parm_idx, in_decl);
8247
8248 if (arg == error_mark_node)
8249 lost++;
8250 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8251 }
8252 cp_unevaluated_operand = saved_unevaluated_operand;
8253 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8254
8255 if (variadic_p && arg_idx < nargs)
8256 {
8257 if (complain & tf_error)
8258 {
8259 error ("wrong number of template arguments "
8260 "(%d, should be %d)", nargs, arg_idx);
8261 if (in_decl)
8262 error ("provided for %q+D", in_decl);
8263 }
8264 return error_mark_node;
8265 }
8266
8267 if (lost)
8268 {
8269 if ((complain & tf_error) && !seen_error())
8270 error ("wrong number of template arguments");
8271 return error_mark_node;
8272 }
8273
8274 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8275 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8276 TREE_VEC_LENGTH (new_inner_args));
8277
8278 return new_inner_args;
8279}
8280
8281/* Convert all template arguments to their appropriate types, and
8282 return a vector containing the innermost resulting template
8283 arguments. If any error occurs, return error_mark_node. Error and
8284 warning messages are not issued.
8285
8286 Note that no function argument deduction is performed, and default
8287 arguments are used to fill in unspecified arguments. */
8288tree
8289coerce_template_parms (tree parms, tree args, tree in_decl)
8290{
8291 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8292}
8293
8294/* Convert all template arguments to their appropriate type, and
8295 instantiate default arguments as needed. This returns a vector
8296 containing the innermost resulting template arguments, or
8297 error_mark_node if unsuccessful. */
8298tree
8299coerce_template_parms (tree parms, tree args, tree in_decl,
8300 tsubst_flags_t complain)
8301{
8302 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8303}
8304
8305/* Like coerce_template_parms. If PARMS represents all template
8306 parameters levels, this function returns a vector of vectors
8307 representing all the resulting argument levels. Note that in this
8308 case, only the innermost arguments are coerced because the
8309 outermost ones are supposed to have been coerced already.
8310
8311 Otherwise, if PARMS represents only (the innermost) vector of
8312 parameters, this function returns a vector containing just the
8313 innermost resulting arguments. */
8314
8315static tree
8316coerce_innermost_template_parms (tree parms,
8317 tree args,
8318 tree in_decl,
8319 tsubst_flags_t complain,
8320 bool require_all_args,
8321 bool use_default_args)
8322{
8323 int parms_depth = TMPL_PARMS_DEPTH (parms);
8324 int args_depth = TMPL_ARGS_DEPTH (args);
8325 tree coerced_args;
8326
8327 if (parms_depth > 1)
8328 {
8329 coerced_args = make_tree_vec (parms_depth);
8330 tree level;
8331 int cur_depth;
8332
8333 for (level = parms, cur_depth = parms_depth;
8334 parms_depth > 0 && level != NULL_TREE;
8335 level = TREE_CHAIN (level), --cur_depth)
8336 {
8337 tree l;
8338 if (cur_depth == args_depth)
8339 l = coerce_template_parms (TREE_VALUE (level),
8340 args, in_decl, complain,
8341 require_all_args,
8342 use_default_args);
8343 else
8344 l = TMPL_ARGS_LEVEL (args, cur_depth);
8345
8346 if (l == error_mark_node)
8347 return error_mark_node;
8348
8349 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8350 }
8351 }
8352 else
8353 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8354 args, in_decl, complain,
8355 require_all_args,
8356 use_default_args);
8357 return coerced_args;
8358}
8359
8360/* Returns 1 if template args OT and NT are equivalent. */
8361
8362int
8363template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8364{
8365 if (nt == ot)
8366 return 1;
8367 if (nt == NULL_TREE || ot == NULL_TREE)
8368 return false;
8369 if (nt == any_targ_node || ot == any_targ_node)
8370 return true;
8371
8372 if (TREE_CODE (nt) == TREE_VEC)
8373 /* For member templates */
8374 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8375 else if (PACK_EXPANSION_P (ot))
8376 return (PACK_EXPANSION_P (nt)
8377 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8378 PACK_EXPANSION_PATTERN (nt))
8379 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8380 PACK_EXPANSION_EXTRA_ARGS (nt)));
8381 else if (ARGUMENT_PACK_P (ot))
8382 {
8383 int i, len;
8384 tree opack, npack;
8385
8386 if (!ARGUMENT_PACK_P (nt))
8387 return 0;
8388
8389 opack = ARGUMENT_PACK_ARGS (ot);
8390 npack = ARGUMENT_PACK_ARGS (nt);
8391 len = TREE_VEC_LENGTH (opack);
8392 if (TREE_VEC_LENGTH (npack) != len)
8393 return 0;
8394 for (i = 0; i < len; ++i)
8395 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8396 TREE_VEC_ELT (npack, i)))
8397 return 0;
8398 return 1;
8399 }
8400 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8401 gcc_unreachable ();
8402 else if (TYPE_P (nt))
8403 {
8404 if (!TYPE_P (ot))
8405 return false;
8406 /* Don't treat an alias template specialization with dependent
8407 arguments as equivalent to its underlying type when used as a
8408 template argument; we need them to be distinct so that we
8409 substitute into the specialization arguments at instantiation
8410 time. And aliases can't be equivalent without being ==, so
8411 we don't need to look any deeper.
8412
8413 During partial ordering, however, we need to treat them normally so
8414 that we can order uses of the same alias with different
8415 cv-qualification (79960). */
8416 if (!partial_order
8417 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8418 return false;
8419 else
8420 return same_type_p (ot, nt);
8421 }
8422 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8423 return 0;
8424 else
8425 {
8426 /* Try to treat a template non-type argument that has been converted
8427 to the parameter type as equivalent to one that hasn't yet. */
8428 for (enum tree_code code1 = TREE_CODE (ot);
8429 CONVERT_EXPR_CODE_P (code1)
8430 || code1 == NON_LVALUE_EXPR;
8431 code1 = TREE_CODE (ot))
8432 ot = TREE_OPERAND (ot, 0);
8433 for (enum tree_code code2 = TREE_CODE (nt);
8434 CONVERT_EXPR_CODE_P (code2)
8435 || code2 == NON_LVALUE_EXPR;
8436 code2 = TREE_CODE (nt))
8437 nt = TREE_OPERAND (nt, 0);
8438
8439 return cp_tree_equal (ot, nt);
8440 }
8441}
8442
8443/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8444 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8445 NEWARG_PTR with the offending arguments if they are non-NULL. */
8446
8447int
8448comp_template_args (tree oldargs, tree newargs,
8449 tree *oldarg_ptr, tree *newarg_ptr,
8450 bool partial_order)
8451{
8452 int i;
8453
8454 if (oldargs == newargs)
8455 return 1;
8456
8457 if (!oldargs || !newargs)
8458 return 0;
8459
8460 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8461 return 0;
8462
8463 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8464 {
8465 tree nt = TREE_VEC_ELT (newargs, i);
8466 tree ot = TREE_VEC_ELT (oldargs, i);
8467
8468 if (! template_args_equal (ot, nt, partial_order))
8469 {
8470 if (oldarg_ptr != NULL)
8471 *oldarg_ptr = ot;
8472 if (newarg_ptr != NULL)
8473 *newarg_ptr = nt;
8474 return 0;
8475 }
8476 }
8477 return 1;
8478}
8479
8480inline bool
8481comp_template_args_porder (tree oargs, tree nargs)
8482{
8483 return comp_template_args (oargs, nargs, NULL, NULL, true);
8484}
8485
8486static void
8487add_pending_template (tree d)
8488{
8489 tree ti = (TYPE_P (d)
8490 ? CLASSTYPE_TEMPLATE_INFO (d)
8491 : DECL_TEMPLATE_INFO (d));
8492 struct pending_template *pt;
8493 int level;
8494
8495 if (TI_PENDING_TEMPLATE_FLAG (ti))
8496 return;
8497
8498 /* We are called both from instantiate_decl, where we've already had a
8499 tinst_level pushed, and instantiate_template, where we haven't.
8500 Compensate. */
8501 level = !current_tinst_level || current_tinst_level->decl != d;
8502
8503 if (level)
8504 push_tinst_level (d);
8505
8506 pt = ggc_alloc<pending_template> ();
8507 pt->next = NULL;
8508 pt->tinst = current_tinst_level;
8509 if (last_pending_template)
8510 last_pending_template->next = pt;
8511 else
8512 pending_templates = pt;
8513
8514 last_pending_template = pt;
8515
8516 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8517
8518 if (level)
8519 pop_tinst_level ();
8520}
8521
8522
8523/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8524 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8525 documentation for TEMPLATE_ID_EXPR. */
8526
8527tree
8528lookup_template_function (tree fns, tree arglist)
8529{
8530 tree type;
8531
8532 if (fns == error_mark_node || arglist == error_mark_node)
8533 return error_mark_node;
8534
8535 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8536
8537 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8538 {
8539 error ("%q#D is not a function template", fns);
8540 return error_mark_node;
8541 }
8542
8543 if (BASELINK_P (fns))
8544 {
8545 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8546 unknown_type_node,
8547 BASELINK_FUNCTIONS (fns),
8548 arglist);
8549 return fns;
8550 }
8551
8552 type = TREE_TYPE (fns);
8553 if (TREE_CODE (fns) == OVERLOAD || !type)
8554 type = unknown_type_node;
8555
8556 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8557}
8558
8559/* Within the scope of a template class S<T>, the name S gets bound
8560 (in build_self_reference) to a TYPE_DECL for the class, not a
8561 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8562 or one of its enclosing classes, and that type is a template,
8563 return the associated TEMPLATE_DECL. Otherwise, the original
8564 DECL is returned.
8565
8566 Also handle the case when DECL is a TREE_LIST of ambiguous
8567 injected-class-names from different bases. */
8568
8569tree
8570maybe_get_template_decl_from_type_decl (tree decl)
8571{
8572 if (decl == NULL_TREE)
8573 return decl;
8574
8575 /* DR 176: A lookup that finds an injected-class-name (10.2
8576 [class.member.lookup]) can result in an ambiguity in certain cases
8577 (for example, if it is found in more than one base class). If all of
8578 the injected-class-names that are found refer to specializations of
8579 the same class template, and if the name is followed by a
8580 template-argument-list, the reference refers to the class template
8581 itself and not a specialization thereof, and is not ambiguous. */
8582 if (TREE_CODE (decl) == TREE_LIST)
8583 {
8584 tree t, tmpl = NULL_TREE;
8585 for (t = decl; t; t = TREE_CHAIN (t))
8586 {
8587 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8588 if (!tmpl)
8589 tmpl = elt;
8590 else if (tmpl != elt)
8591 break;
8592 }
8593 if (tmpl && t == NULL_TREE)
8594 return tmpl;
8595 else
8596 return decl;
8597 }
8598
8599 return (decl != NULL_TREE
8600 && DECL_SELF_REFERENCE_P (decl)
8601 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8602 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8603}
8604
8605/* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8606 parameters, find the desired type.
8607
8608 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8609
8610 IN_DECL, if non-NULL, is the template declaration we are trying to
8611 instantiate.
8612
8613 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8614 the class we are looking up.
8615
8616 Issue error and warning messages under control of COMPLAIN.
8617
8618 If the template class is really a local class in a template
8619 function, then the FUNCTION_CONTEXT is the function in which it is
8620 being instantiated.
8621
8622 ??? Note that this function is currently called *twice* for each
8623 template-id: the first time from the parser, while creating the
8624 incomplete type (finish_template_type), and the second type during the
8625 real instantiation (instantiate_template_class). This is surely something
8626 that we want to avoid. It also causes some problems with argument
8627 coercion (see convert_nontype_argument for more information on this). */
8628
8629static tree
8630lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8631 int entering_scope, tsubst_flags_t complain)
8632{
8633 tree templ = NULL_TREE, parmlist;
8634 tree t;
8635 spec_entry **slot;
8636 spec_entry *entry;
8637 spec_entry elt;
8638 hashval_t hash;
8639
8640 if (identifier_p (d1))
8641 {
8642 tree value = innermost_non_namespace_value (d1);
8643 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8644 templ = value;
8645 else
8646 {
8647 if (context)
8648 push_decl_namespace (context);
8649 templ = lookup_name (d1);
8650 templ = maybe_get_template_decl_from_type_decl (templ);
8651 if (context)
8652 pop_decl_namespace ();
8653 }
8654 if (templ)
8655 context = DECL_CONTEXT (templ);
8656 }
8657 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8658 {
8659 tree type = TREE_TYPE (d1);
8660
8661 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8662 an implicit typename for the second A. Deal with it. */
8663 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8664 type = TREE_TYPE (type);
8665
8666 if (CLASSTYPE_TEMPLATE_INFO (type))
8667 {
8668 templ = CLASSTYPE_TI_TEMPLATE (type);
8669 d1 = DECL_NAME (templ);
8670 }
8671 }
8672 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8673 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8674 {
8675 templ = TYPE_TI_TEMPLATE (d1);
8676 d1 = DECL_NAME (templ);
8677 }
8678 else if (DECL_TYPE_TEMPLATE_P (d1))
8679 {
8680 templ = d1;
8681 d1 = DECL_NAME (templ);
8682 context = DECL_CONTEXT (templ);
8683 }
8684 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8685 {
8686 templ = d1;
8687 d1 = DECL_NAME (templ);
8688 }
8689
8690 /* Issue an error message if we didn't find a template. */
8691 if (! templ)
8692 {
8693 if (complain & tf_error)
8694 error ("%qT is not a template", d1);
8695 return error_mark_node;
8696 }
8697
8698 if (TREE_CODE (templ) != TEMPLATE_DECL
8699 /* Make sure it's a user visible template, if it was named by
8700 the user. */
8701 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8702 && !PRIMARY_TEMPLATE_P (templ)))
8703 {
8704 if (complain & tf_error)
8705 {
8706 error ("non-template type %qT used as a template", d1);
8707 if (in_decl)
8708 error ("for template declaration %q+D", in_decl);
8709 }
8710 return error_mark_node;
8711 }
8712
8713 complain &= ~tf_user;
8714
8715 /* An alias that just changes the name of a template is equivalent to the
8716 other template, so if any of the arguments are pack expansions, strip
8717 the alias to avoid problems with a pack expansion passed to a non-pack
8718 alias template parameter (DR 1430). */
8719 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8720 templ = get_underlying_template (templ);
8721
8722 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8723 {
8724 tree parm;
8725 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8726 if (arglist2 == error_mark_node
8727 || (!uses_template_parms (arglist2)
8728 && check_instantiated_args (templ, arglist2, complain)))
8729 return error_mark_node;
8730
8731 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8732 return parm;
8733 }
8734 else
8735 {
8736 tree template_type = TREE_TYPE (templ);
8737 tree gen_tmpl;
8738 tree type_decl;
8739 tree found = NULL_TREE;
8740 int arg_depth;
8741 int parm_depth;
8742 int is_dependent_type;
8743 int use_partial_inst_tmpl = false;
8744
8745 if (template_type == error_mark_node)
8746 /* An error occurred while building the template TEMPL, and a
8747 diagnostic has most certainly been emitted for that
8748 already. Let's propagate that error. */
8749 return error_mark_node;
8750
8751 gen_tmpl = most_general_template (templ);
8752 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8753 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8754 arg_depth = TMPL_ARGS_DEPTH (arglist);
8755
8756 if (arg_depth == 1 && parm_depth > 1)
8757 {
8758 /* We've been given an incomplete set of template arguments.
8759 For example, given:
8760
8761 template <class T> struct S1 {
8762 template <class U> struct S2 {};
8763 template <class U> struct S2<U*> {};
8764 };
8765
8766 we will be called with an ARGLIST of `U*', but the
8767 TEMPLATE will be `template <class T> template
8768 <class U> struct S1<T>::S2'. We must fill in the missing
8769 arguments. */
8770 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8771 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8772 arg_depth = TMPL_ARGS_DEPTH (arglist);
8773 }
8774
8775 /* Now we should have enough arguments. */
8776 gcc_assert (parm_depth == arg_depth);
8777
8778 /* From here on, we're only interested in the most general
8779 template. */
8780
8781 /* Calculate the BOUND_ARGS. These will be the args that are
8782 actually tsubst'd into the definition to create the
8783 instantiation. */
8784 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8785 complain,
8786 /*require_all_args=*/true,
8787 /*use_default_args=*/true);
8788
8789 if (arglist == error_mark_node)
8790 /* We were unable to bind the arguments. */
8791 return error_mark_node;
8792
8793 /* In the scope of a template class, explicit references to the
8794 template class refer to the type of the template, not any
8795 instantiation of it. For example, in:
8796
8797 template <class T> class C { void f(C<T>); }
8798
8799 the `C<T>' is just the same as `C'. Outside of the
8800 class, however, such a reference is an instantiation. */
8801 if (entering_scope
8802 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8803 || currently_open_class (template_type))
8804 {
8805 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8806
8807 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8808 return template_type;
8809 }
8810
8811 /* If we already have this specialization, return it. */
8812 elt.tmpl = gen_tmpl;
8813 elt.args = arglist;
8814 elt.spec = NULL_TREE;
8815 hash = spec_hasher::hash (&elt);
8816 entry = type_specializations->find_with_hash (&elt, hash);
8817
8818 if (entry)
8819 return entry->spec;
8820
8821 /* If the the template's constraints are not satisfied,
8822 then we cannot form a valid type.
8823
8824 Note that the check is deferred until after the hash
8825 lookup. This prevents redundant checks on previously
8826 instantiated specializations. */
8827 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8828 {
8829 if (complain & tf_error)
8830 {
8831 error ("template constraint failure");
8832 diagnose_constraints (input_location, gen_tmpl, arglist);
8833 }
8834 return error_mark_node;
8835 }
8836
8837 is_dependent_type = uses_template_parms (arglist);
8838
8839 /* If the deduced arguments are invalid, then the binding
8840 failed. */
8841 if (!is_dependent_type
8842 && check_instantiated_args (gen_tmpl,
8843 INNERMOST_TEMPLATE_ARGS (arglist),
8844 complain))
8845 return error_mark_node;
8846
8847 if (!is_dependent_type
8848 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8849 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8850 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8851 {
8852 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8853 DECL_NAME (gen_tmpl),
8854 /*tag_scope=*/ts_global);
8855 return found;
8856 }
8857
8858 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8859 complain, in_decl);
8860 if (context == error_mark_node)
8861 return error_mark_node;
8862
8863 if (!context)
8864 context = global_namespace;
8865
8866 /* Create the type. */
8867 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8868 {
8869 /* The user referred to a specialization of an alias
8870 template represented by GEN_TMPL.
8871
8872 [temp.alias]/2 says:
8873
8874 When a template-id refers to the specialization of an
8875 alias template, it is equivalent to the associated
8876 type obtained by substitution of its
8877 template-arguments for the template-parameters in the
8878 type-id of the alias template. */
8879
8880 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8881 /* Note that the call above (by indirectly calling
8882 register_specialization in tsubst_decl) registers the
8883 TYPE_DECL representing the specialization of the alias
8884 template. So next time someone substitutes ARGLIST for
8885 the template parms into the alias template (GEN_TMPL),
8886 she'll get that TYPE_DECL back. */
8887
8888 if (t == error_mark_node)
8889 return t;
8890 }
8891 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8892 {
8893 if (!is_dependent_type)
8894 {
8895 set_current_access_from_decl (TYPE_NAME (template_type));
8896 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8897 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8898 arglist, complain, in_decl),
8899 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8900 arglist, complain, in_decl),
8901 SCOPED_ENUM_P (template_type), NULL);
8902
8903 if (t == error_mark_node)
8904 return t;
8905 }
8906 else
8907 {
8908 /* We don't want to call start_enum for this type, since
8909 the values for the enumeration constants may involve
8910 template parameters. And, no one should be interested
8911 in the enumeration constants for such a type. */
8912 t = cxx_make_type (ENUMERAL_TYPE);
8913 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8914 }
8915 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8916 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8917 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8918 }
8919 else if (CLASS_TYPE_P (template_type))
8920 {
8921 t = make_class_type (TREE_CODE (template_type));
8922 CLASSTYPE_DECLARED_CLASS (t)
8923 = CLASSTYPE_DECLARED_CLASS (template_type);
8924 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8925
8926 /* A local class. Make sure the decl gets registered properly. */
8927 if (context == current_function_decl)
8928 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8929
8930 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8931 /* This instantiation is another name for the primary
8932 template type. Set the TYPE_CANONICAL field
8933 appropriately. */
8934 TYPE_CANONICAL (t) = template_type;
8935 else if (any_template_arguments_need_structural_equality_p (arglist))
8936 /* Some of the template arguments require structural
8937 equality testing, so this template class requires
8938 structural equality testing. */
8939 SET_TYPE_STRUCTURAL_EQUALITY (t);
8940 }
8941 else
8942 gcc_unreachable ();
8943
8944 /* If we called start_enum or pushtag above, this information
8945 will already be set up. */
8946 if (!TYPE_NAME (t))
8947 {
8948 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8949
8950 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8951 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8952 DECL_SOURCE_LOCATION (type_decl)
8953 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8954 }
8955 else
8956 type_decl = TYPE_NAME (t);
8957
8958 if (CLASS_TYPE_P (template_type))
8959 {
8960 TREE_PRIVATE (type_decl)
8961 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8962 TREE_PROTECTED (type_decl)
8963 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8964 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8965 {
8966 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8967 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8968 }
8969 }
8970
8971 if (OVERLOAD_TYPE_P (t)
8972 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8973 {
8974 static const char *tags[] = {"abi_tag", "may_alias"};
8975
8976 for (unsigned ix = 0; ix != 2; ix++)
8977 {
8978 tree attributes
8979 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8980
8981 if (attributes)
8982 TYPE_ATTRIBUTES (t)
8983 = tree_cons (TREE_PURPOSE (attributes),
8984 TREE_VALUE (attributes),
8985 TYPE_ATTRIBUTES (t));
8986 }
8987 }
8988
8989 /* Let's consider the explicit specialization of a member
8990 of a class template specialization that is implicitly instantiated,
8991 e.g.:
8992 template<class T>
8993 struct S
8994 {
8995 template<class U> struct M {}; //#0
8996 };
8997
8998 template<>
8999 template<>
9000 struct S<int>::M<char> //#1
9001 {
9002 int i;
9003 };
9004 [temp.expl.spec]/4 says this is valid.
9005
9006 In this case, when we write:
9007 S<int>::M<char> m;
9008
9009 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9010 the one of #0.
9011
9012 When we encounter #1, we want to store the partial instantiation
9013 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9014
9015 For all cases other than this "explicit specialization of member of a
9016 class template", we just want to store the most general template into
9017 the CLASSTYPE_TI_TEMPLATE of M.
9018
9019 This case of "explicit specialization of member of a class template"
9020 only happens when:
9021 1/ the enclosing class is an instantiation of, and therefore not
9022 the same as, the context of the most general template, and
9023 2/ we aren't looking at the partial instantiation itself, i.e.
9024 the innermost arguments are not the same as the innermost parms of
9025 the most general template.
9026
9027 So it's only when 1/ and 2/ happens that we want to use the partial
9028 instantiation of the member template in lieu of its most general
9029 template. */
9030
9031 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9032 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9033 /* the enclosing class must be an instantiation... */
9034 && CLASS_TYPE_P (context)
9035 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9036 {
9037 TREE_VEC_LENGTH (arglist)--;
9038 ++processing_template_decl;
9039 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9040 tree partial_inst_args =
9041 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9042 arglist, complain, NULL_TREE);
9043 --processing_template_decl;
9044 TREE_VEC_LENGTH (arglist)++;
9045 if (partial_inst_args == error_mark_node)
9046 return error_mark_node;
9047 use_partial_inst_tmpl =
9048 /*...and we must not be looking at the partial instantiation
9049 itself. */
9050 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9051 partial_inst_args);
9052 }
9053
9054 if (!use_partial_inst_tmpl)
9055 /* This case is easy; there are no member templates involved. */
9056 found = gen_tmpl;
9057 else
9058 {
9059 /* This is a full instantiation of a member template. Find
9060 the partial instantiation of which this is an instance. */
9061
9062 /* Temporarily reduce by one the number of levels in the ARGLIST
9063 so as to avoid comparing the last set of arguments. */
9064 TREE_VEC_LENGTH (arglist)--;
9065 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9066 TREE_VEC_LENGTH (arglist)++;
9067 /* FOUND is either a proper class type, or an alias
9068 template specialization. In the later case, it's a
9069 TYPE_DECL, resulting from the substituting of arguments
9070 for parameters in the TYPE_DECL of the alias template
9071 done earlier. So be careful while getting the template
9072 of FOUND. */
9073 found = (TREE_CODE (found) == TEMPLATE_DECL
9074 ? found
9075 : (TREE_CODE (found) == TYPE_DECL
9076 ? DECL_TI_TEMPLATE (found)
9077 : CLASSTYPE_TI_TEMPLATE (found)));
9078 }
9079
9080 // Build template info for the new specialization.
9081 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9082
9083 elt.spec = t;
9084 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9085 entry = ggc_alloc<spec_entry> ();
9086 *entry = elt;
9087 *slot = entry;
9088
9089 /* Note this use of the partial instantiation so we can check it
9090 later in maybe_process_partial_specialization. */
9091 DECL_TEMPLATE_INSTANTIATIONS (found)
9092 = tree_cons (arglist, t,
9093 DECL_TEMPLATE_INSTANTIATIONS (found));
9094
9095 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9096 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9097 /* Now that the type has been registered on the instantiations
9098 list, we set up the enumerators. Because the enumeration
9099 constants may involve the enumeration type itself, we make
9100 sure to register the type first, and then create the
9101 constants. That way, doing tsubst_expr for the enumeration
9102 constants won't result in recursive calls here; we'll find
9103 the instantiation and exit above. */
9104 tsubst_enum (template_type, t, arglist);
9105
9106 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9107 /* If the type makes use of template parameters, the
9108 code that generates debugging information will crash. */
9109 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9110
9111 /* Possibly limit visibility based on template args. */
9112 TREE_PUBLIC (type_decl) = 1;
9113 determine_visibility (type_decl);
9114
9115 inherit_targ_abi_tags (t);
9116
9117 return t;
9118 }
9119}
9120
9121/* Wrapper for lookup_template_class_1. */
9122
9123tree
9124lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9125 int entering_scope, tsubst_flags_t complain)
9126{
9127 tree ret;
9128 timevar_push (TV_TEMPLATE_INST);
9129 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9130 entering_scope, complain);
9131 timevar_pop (TV_TEMPLATE_INST);
9132 return ret;
9133}
9134
9135/* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9136
9137tree
9138lookup_template_variable (tree templ, tree arglist)
9139{
9140 /* The type of the expression is NULL_TREE since the template-id could refer
9141 to an explicit or partial specialization. */
9142 tree type = NULL_TREE;
9143 if (flag_concepts && variable_concept_p (templ))
9144 /* Except that concepts are always bool. */
9145 type = boolean_type_node;
9146 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9147}
9148
9149/* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9150
9151tree
9152finish_template_variable (tree var, tsubst_flags_t complain)
9153{
9154 tree templ = TREE_OPERAND (var, 0);
9155 tree arglist = TREE_OPERAND (var, 1);
9156
9157 /* We never want to return a VAR_DECL for a variable concept, since they
9158 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9159 bool concept_p = flag_concepts && variable_concept_p (templ);
9160 if (concept_p && processing_template_decl)
9161 return var;
9162
9163 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9164 arglist = add_outermost_template_args (tmpl_args, arglist);
9165
9166 templ = most_general_template (templ);
9167 tree parms = DECL_TEMPLATE_PARMS (templ);
9168 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9169 /*req_all*/true,
9170 /*use_default*/true);
9171
9172 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9173 {
9174 if (complain & tf_error)
9175 {
9176 error ("use of invalid variable template %qE", var);
9177 diagnose_constraints (location_of (var), templ, arglist);
9178 }
9179 return error_mark_node;
9180 }
9181
9182 /* If a template-id refers to a specialization of a variable
9183 concept, then the expression is true if and only if the
9184 concept's constraints are satisfied by the given template
9185 arguments.
9186
9187 NOTE: This is an extension of Concepts Lite TS that
9188 allows constraints to be used in expressions. */
9189 if (concept_p)
9190 {
9191 tree decl = DECL_TEMPLATE_RESULT (templ);
9192 return evaluate_variable_concept (decl, arglist);
9193 }
9194
9195 return instantiate_template (templ, arglist, complain);
9196}
9197
9198/* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9199 TARGS template args, and instantiate it if it's not dependent. */
9200
9201tree
9202lookup_and_finish_template_variable (tree templ, tree targs,
9203 tsubst_flags_t complain)
9204{
9205 templ = lookup_template_variable (templ, targs);
9206 if (!any_dependent_template_arguments_p (targs))
9207 {
9208 templ = finish_template_variable (templ, complain);
9209 mark_used (templ);
9210 }
9211
9212 return convert_from_reference (templ);
9213}
9214
9215
9216struct pair_fn_data
9217{
9218 tree_fn_t fn;
9219 tree_fn_t any_fn;
9220 void *data;
9221 /* True when we should also visit template parameters that occur in
9222 non-deduced contexts. */
9223 bool include_nondeduced_p;
9224 hash_set<tree> *visited;
9225};
9226
9227/* Called from for_each_template_parm via walk_tree. */
9228
9229static tree
9230for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9231{
9232 tree t = *tp;
9233 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9234 tree_fn_t fn = pfd->fn;
9235 void *data = pfd->data;
9236 tree result = NULL_TREE;
9237
9238#define WALK_SUBTREE(NODE) \
9239 do \
9240 { \
9241 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9242 pfd->include_nondeduced_p, \
9243 pfd->any_fn); \
9244 if (result) goto out; \
9245 } \
9246 while (0)
9247
9248 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9249 return t;
9250
9251 if (TYPE_P (t)
9252 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9253 WALK_SUBTREE (TYPE_CONTEXT (t));
9254
9255 switch (TREE_CODE (t))
9256 {
9257 case RECORD_TYPE:
9258 if (TYPE_PTRMEMFUNC_P (t))
9259 break;
9260 /* Fall through. */
9261
9262 case UNION_TYPE:
9263 case ENUMERAL_TYPE:
9264 if (!TYPE_TEMPLATE_INFO (t))
9265 *walk_subtrees = 0;
9266 else
9267 WALK_SUBTREE (TYPE_TI_ARGS (t));
9268 break;
9269
9270 case INTEGER_TYPE:
9271 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9272 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9273 break;
9274
9275 case METHOD_TYPE:
9276 /* Since we're not going to walk subtrees, we have to do this
9277 explicitly here. */
9278 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9279 /* Fall through. */
9280
9281 case FUNCTION_TYPE:
9282 /* Check the return type. */
9283 WALK_SUBTREE (TREE_TYPE (t));
9284
9285 /* Check the parameter types. Since default arguments are not
9286 instantiated until they are needed, the TYPE_ARG_TYPES may
9287 contain expressions that involve template parameters. But,
9288 no-one should be looking at them yet. And, once they're
9289 instantiated, they don't contain template parameters, so
9290 there's no point in looking at them then, either. */
9291 {
9292 tree parm;
9293
9294 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9295 WALK_SUBTREE (TREE_VALUE (parm));
9296
9297 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9298 want walk_tree walking into them itself. */
9299 *walk_subtrees = 0;
9300 }
9301
9302 if (flag_noexcept_type)
9303 {
9304 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9305 if (spec)
9306 WALK_SUBTREE (TREE_PURPOSE (spec));
9307 }
9308 break;
9309
9310 case TYPEOF_TYPE:
9311 case UNDERLYING_TYPE:
9312 if (pfd->include_nondeduced_p
9313 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9314 pfd->visited,
9315 pfd->include_nondeduced_p,
9316 pfd->any_fn))
9317 return error_mark_node;
9318 break;
9319
9320 case FUNCTION_DECL:
9321 case VAR_DECL:
9322 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9323 WALK_SUBTREE (DECL_TI_ARGS (t));
9324 /* Fall through. */
9325
9326 case PARM_DECL:
9327 case CONST_DECL:
9328 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9329 WALK_SUBTREE (DECL_INITIAL (t));
9330 if (DECL_CONTEXT (t)
9331 && pfd->include_nondeduced_p)
9332 WALK_SUBTREE (DECL_CONTEXT (t));
9333 break;
9334
9335 case BOUND_TEMPLATE_TEMPLATE_PARM:
9336 /* Record template parameters such as `T' inside `TT<T>'. */
9337 WALK_SUBTREE (TYPE_TI_ARGS (t));
9338 /* Fall through. */
9339
9340 case TEMPLATE_TEMPLATE_PARM:
9341 case TEMPLATE_TYPE_PARM:
9342 case TEMPLATE_PARM_INDEX:
9343 if (fn && (*fn)(t, data))
9344 return t;
9345 else if (!fn)
9346 return t;
9347 break;
9348
9349 case TEMPLATE_DECL:
9350 /* A template template parameter is encountered. */
9351 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9352 WALK_SUBTREE (TREE_TYPE (t));
9353
9354 /* Already substituted template template parameter */
9355 *walk_subtrees = 0;
9356 break;
9357
9358 case TYPENAME_TYPE:
9359 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9360 partial instantiation. */
9361 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9362 break;
9363
9364 case CONSTRUCTOR:
9365 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9366 && pfd->include_nondeduced_p)
9367 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9368 break;
9369
9370 case INDIRECT_REF:
9371 case COMPONENT_REF:
9372 /* If there's no type, then this thing must be some expression
9373 involving template parameters. */
9374 if (!fn && !TREE_TYPE (t))
9375 return error_mark_node;
9376 break;
9377
9378 case MODOP_EXPR:
9379 case CAST_EXPR:
9380 case IMPLICIT_CONV_EXPR:
9381 case REINTERPRET_CAST_EXPR:
9382 case CONST_CAST_EXPR:
9383 case STATIC_CAST_EXPR:
9384 case DYNAMIC_CAST_EXPR:
9385 case ARROW_EXPR:
9386 case DOTSTAR_EXPR:
9387 case TYPEID_EXPR:
9388 case PSEUDO_DTOR_EXPR:
9389 if (!fn)
9390 return error_mark_node;
9391 break;
9392
9393 default:
9394 break;
9395 }
9396
9397 #undef WALK_SUBTREE
9398
9399 /* We didn't find any template parameters we liked. */
9400 out:
9401 return result;
9402}
9403
9404/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9405 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9406 call FN with the parameter and the DATA.
9407 If FN returns nonzero, the iteration is terminated, and
9408 for_each_template_parm returns 1. Otherwise, the iteration
9409 continues. If FN never returns a nonzero value, the value
9410 returned by for_each_template_parm is 0. If FN is NULL, it is
9411 considered to be the function which always returns 1.
9412
9413 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9414 parameters that occur in non-deduced contexts. When false, only
9415 visits those template parameters that can be deduced. */
9416
9417static tree
9418for_each_template_parm (tree t, tree_fn_t fn, void* data,
9419 hash_set<tree> *visited,
9420 bool include_nondeduced_p,
9421 tree_fn_t any_fn)
9422{
9423 struct pair_fn_data pfd;
9424 tree result;
9425
9426 /* Set up. */
9427 pfd.fn = fn;
9428 pfd.any_fn = any_fn;
9429 pfd.data = data;
9430 pfd.include_nondeduced_p = include_nondeduced_p;
9431
9432 /* Walk the tree. (Conceptually, we would like to walk without
9433 duplicates, but for_each_template_parm_r recursively calls
9434 for_each_template_parm, so we would need to reorganize a fair
9435 bit to use walk_tree_without_duplicates, so we keep our own
9436 visited list.) */
9437 if (visited)
9438 pfd.visited = visited;
9439 else
9440 pfd.visited = new hash_set<tree>;
9441 result = cp_walk_tree (&t,
9442 for_each_template_parm_r,
9443 &pfd,
9444 pfd.visited);
9445
9446 /* Clean up. */
9447 if (!visited)
9448 {
9449 delete pfd.visited;
9450 pfd.visited = 0;
9451 }
9452
9453 return result;
9454}
9455
9456/* Returns true if T depends on any template parameter. */
9457
9458int
9459uses_template_parms (tree t)
9460{
9461 if (t == NULL_TREE)
9462 return false;
9463
9464 bool dependent_p;
9465 int saved_processing_template_decl;
9466
9467 saved_processing_template_decl = processing_template_decl;
9468 if (!saved_processing_template_decl)
9469 processing_template_decl = 1;
9470 if (TYPE_P (t))
9471 dependent_p = dependent_type_p (t);
9472 else if (TREE_CODE (t) == TREE_VEC)
9473 dependent_p = any_dependent_template_arguments_p (t);
9474 else if (TREE_CODE (t) == TREE_LIST)
9475 dependent_p = (uses_template_parms (TREE_VALUE (t))
9476 || uses_template_parms (TREE_CHAIN (t)));
9477 else if (TREE_CODE (t) == TYPE_DECL)
9478 dependent_p = dependent_type_p (TREE_TYPE (t));
9479 else if (DECL_P (t)
9480 || EXPR_P (t)
9481 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9482 || TREE_CODE (t) == OVERLOAD
9483 || BASELINK_P (t)
9484 || identifier_p (t)
9485 || TREE_CODE (t) == TRAIT_EXPR
9486 || TREE_CODE (t) == CONSTRUCTOR
9487 || CONSTANT_CLASS_P (t))
9488 dependent_p = (type_dependent_expression_p (t)
9489 || value_dependent_expression_p (t));
9490 else
9491 {
9492 gcc_assert (t == error_mark_node);
9493 dependent_p = false;
9494 }
9495
9496 processing_template_decl = saved_processing_template_decl;
9497
9498 return dependent_p;
9499}
9500
9501/* Returns true iff current_function_decl is an incompletely instantiated
9502 template. Useful instead of processing_template_decl because the latter
9503 is set to 0 during instantiate_non_dependent_expr. */
9504
9505bool
9506in_template_function (void)
9507{
9508 tree fn = current_function_decl;
9509 bool ret;
9510 ++processing_template_decl;
9511 ret = (fn && DECL_LANG_SPECIFIC (fn)
9512 && DECL_TEMPLATE_INFO (fn)
9513 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9514 --processing_template_decl;
9515 return ret;
9516}
9517
9518/* Returns true if T depends on any template parameter with level LEVEL. */
9519
9520bool
9521uses_template_parms_level (tree t, int level)
9522{
9523 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9524 /*include_nondeduced_p=*/true);
9525}
9526
9527/* Returns true if the signature of DECL depends on any template parameter from
9528 its enclosing class. */
9529
9530bool
9531uses_outer_template_parms (tree decl)
9532{
9533 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9534 if (depth == 0)
9535 return false;
9536 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9537 &depth, NULL, /*include_nondeduced_p=*/true))
9538 return true;
9539 if (PRIMARY_TEMPLATE_P (decl)
9540 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9541 (DECL_TEMPLATE_PARMS (decl)),
9542 template_parm_outer_level,
9543 &depth, NULL, /*include_nondeduced_p=*/true))
9544 return true;
9545 tree ci = get_constraints (decl);
9546 if (ci)
9547 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9548 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9549 &depth, NULL, /*nondeduced*/true))
9550 return true;
9551 return false;
9552}
9553
9554/* Returns TRUE iff INST is an instantiation we don't need to do in an
9555 ill-formed translation unit, i.e. a variable or function that isn't
9556 usable in a constant expression. */
9557
9558static inline bool
9559neglectable_inst_p (tree d)
9560{
9561 return (DECL_P (d)
9562 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9563 : decl_maybe_constant_var_p (d)));
9564}
9565
9566/* Returns TRUE iff we should refuse to instantiate DECL because it's
9567 neglectable and instantiated from within an erroneous instantiation. */
9568
9569static bool
9570limit_bad_template_recursion (tree decl)
9571{
9572 struct tinst_level *lev = current_tinst_level;
9573 int errs = errorcount + sorrycount;
9574 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9575 return false;
9576
9577 for (; lev; lev = lev->next)
9578 if (neglectable_inst_p (lev->decl))
9579 break;
9580
9581 return (lev && errs > lev->errors);
9582}
9583
9584static int tinst_depth;
9585extern int max_tinst_depth;
9586int depth_reached;
9587
9588static GTY(()) struct tinst_level *last_error_tinst_level;
9589
9590/* We're starting to instantiate D; record the template instantiation context
9591 for diagnostics and to restore it later. */
9592
9593bool
9594push_tinst_level (tree d)
9595{
9596 return push_tinst_level_loc (d, input_location);
9597}
9598
9599/* We're starting to instantiate D; record the template instantiation context
9600 at LOC for diagnostics and to restore it later. */
9601
9602bool
9603push_tinst_level_loc (tree d, location_t loc)
9604{
9605 struct tinst_level *new_level;
9606
9607 if (tinst_depth >= max_tinst_depth)
9608 {
9609 /* Tell error.c not to try to instantiate any templates. */
9610 at_eof = 2;
9611 fatal_error (input_location,
9612 "template instantiation depth exceeds maximum of %d"
9613 " (use -ftemplate-depth= to increase the maximum)",
9614 max_tinst_depth);
9615 return false;
9616 }
9617
9618 /* If the current instantiation caused problems, don't let it instantiate
9619 anything else. Do allow deduction substitution and decls usable in
9620 constant expressions. */
9621 if (limit_bad_template_recursion (d))
9622 return false;
9623
9624 /* When not -quiet, dump template instantiations other than functions, since
9625 announce_function will take care of those. */
9626 if (!quiet_flag
9627 && TREE_CODE (d) != TREE_LIST
9628 && TREE_CODE (d) != FUNCTION_DECL)
9629 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9630
9631 new_level = ggc_alloc<tinst_level> ();
9632 new_level->decl = d;
9633 new_level->locus = loc;
9634 new_level->errors = errorcount+sorrycount;
9635 new_level->in_system_header_p = in_system_header_at (input_location);
9636 new_level->next = current_tinst_level;
9637 current_tinst_level = new_level;
9638
9639 ++tinst_depth;
9640 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9641 depth_reached = tinst_depth;
9642
9643 return true;
9644}
9645
9646/* We're done instantiating this template; return to the instantiation
9647 context. */
9648
9649void
9650pop_tinst_level (void)
9651{
9652 /* Restore the filename and line number stashed away when we started
9653 this instantiation. */
9654 input_location = current_tinst_level->locus;
9655 current_tinst_level = current_tinst_level->next;
9656 --tinst_depth;
9657}
9658
9659/* We're instantiating a deferred template; restore the template
9660 instantiation context in which the instantiation was requested, which
9661 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9662
9663static tree
9664reopen_tinst_level (struct tinst_level *level)
9665{
9666 struct tinst_level *t;
9667
9668 tinst_depth = 0;
9669 for (t = level; t; t = t->next)
9670 ++tinst_depth;
9671
9672 current_tinst_level = level;
9673 pop_tinst_level ();
9674 if (current_tinst_level)
9675 current_tinst_level->errors = errorcount+sorrycount;
9676 return level->decl;
9677}
9678
9679/* Returns the TINST_LEVEL which gives the original instantiation
9680 context. */
9681
9682struct tinst_level *
9683outermost_tinst_level (void)
9684{
9685 struct tinst_level *level = current_tinst_level;
9686 if (level)
9687 while (level->next)
9688 level = level->next;
9689 return level;
9690}
9691
9692/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9693 vector of template arguments, as for tsubst.
9694
9695 Returns an appropriate tsubst'd friend declaration. */
9696
9697static tree
9698tsubst_friend_function (tree decl, tree args)
9699{
9700 tree new_friend;
9701
9702 if (TREE_CODE (decl) == FUNCTION_DECL
9703 && DECL_TEMPLATE_INSTANTIATION (decl)
9704 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9705 /* This was a friend declared with an explicit template
9706 argument list, e.g.:
9707
9708 friend void f<>(T);
9709
9710 to indicate that f was a template instantiation, not a new
9711 function declaration. Now, we have to figure out what
9712 instantiation of what template. */
9713 {
9714 tree template_id, arglist, fns;
9715 tree new_args;
9716 tree tmpl;
9717 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9718
9719 /* Friend functions are looked up in the containing namespace scope.
9720 We must enter that scope, to avoid finding member functions of the
9721 current class with same name. */
9722 push_nested_namespace (ns);
9723 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9724 tf_warning_or_error, NULL_TREE,
9725 /*integral_constant_expression_p=*/false);
9726 pop_nested_namespace (ns);
9727 arglist = tsubst (DECL_TI_ARGS (decl), args,
9728 tf_warning_or_error, NULL_TREE);
9729 template_id = lookup_template_function (fns, arglist);
9730
9731 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9732 tmpl = determine_specialization (template_id, new_friend,
9733 &new_args,
9734 /*need_member_template=*/0,
9735 TREE_VEC_LENGTH (args),
9736 tsk_none);
9737 return instantiate_template (tmpl, new_args, tf_error);
9738 }
9739
9740 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9741
9742 /* The NEW_FRIEND will look like an instantiation, to the
9743 compiler, but is not an instantiation from the point of view of
9744 the language. For example, we might have had:
9745
9746 template <class T> struct S {
9747 template <class U> friend void f(T, U);
9748 };
9749
9750 Then, in S<int>, template <class U> void f(int, U) is not an
9751 instantiation of anything. */
9752 if (new_friend == error_mark_node)
9753 return error_mark_node;
9754
9755 DECL_USE_TEMPLATE (new_friend) = 0;
9756 if (TREE_CODE (decl) == TEMPLATE_DECL)
9757 {
9758 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9759 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9760 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9761 }
9762
9763 /* The mangled name for the NEW_FRIEND is incorrect. The function
9764 is not a template instantiation and should not be mangled like
9765 one. Therefore, we forget the mangling here; we'll recompute it
9766 later if we need it. */
9767 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9768 {
9769 SET_DECL_RTL (new_friend, NULL);
9770 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9771 }
9772
9773 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9774 {
9775 tree old_decl;
9776 tree new_friend_template_info;
9777 tree new_friend_result_template_info;
9778 tree ns;
9779 int new_friend_is_defn;
9780
9781 /* We must save some information from NEW_FRIEND before calling
9782 duplicate decls since that function will free NEW_FRIEND if
9783 possible. */
9784 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9785 new_friend_is_defn =
9786 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9787 (template_for_substitution (new_friend)))
9788 != NULL_TREE);
9789 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9790 {
9791 /* This declaration is a `primary' template. */
9792 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9793
9794 new_friend_result_template_info
9795 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9796 }
9797 else
9798 new_friend_result_template_info = NULL_TREE;
9799
9800 /* Inside pushdecl_namespace_level, we will push into the
9801 current namespace. However, the friend function should go
9802 into the namespace of the template. */
9803 ns = decl_namespace_context (new_friend);
9804 push_nested_namespace (ns);
9805 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9806 pop_nested_namespace (ns);
9807
9808 if (old_decl == error_mark_node)
9809 return error_mark_node;
9810
9811 if (old_decl != new_friend)
9812 {
9813 /* This new friend declaration matched an existing
9814 declaration. For example, given:
9815
9816 template <class T> void f(T);
9817 template <class U> class C {
9818 template <class T> friend void f(T) {}
9819 };
9820
9821 the friend declaration actually provides the definition
9822 of `f', once C has been instantiated for some type. So,
9823 old_decl will be the out-of-class template declaration,
9824 while new_friend is the in-class definition.
9825
9826 But, if `f' was called before this point, the
9827 instantiation of `f' will have DECL_TI_ARGS corresponding
9828 to `T' but not to `U', references to which might appear
9829 in the definition of `f'. Previously, the most general
9830 template for an instantiation of `f' was the out-of-class
9831 version; now it is the in-class version. Therefore, we
9832 run through all specialization of `f', adding to their
9833 DECL_TI_ARGS appropriately. In particular, they need a
9834 new set of outer arguments, corresponding to the
9835 arguments for this class instantiation.
9836
9837 The same situation can arise with something like this:
9838
9839 friend void f(int);
9840 template <class T> class C {
9841 friend void f(T) {}
9842 };
9843
9844 when `C<int>' is instantiated. Now, `f(int)' is defined
9845 in the class. */
9846
9847 if (!new_friend_is_defn)
9848 /* On the other hand, if the in-class declaration does
9849 *not* provide a definition, then we don't want to alter
9850 existing definitions. We can just leave everything
9851 alone. */
9852 ;
9853 else
9854 {
9855 tree new_template = TI_TEMPLATE (new_friend_template_info);
9856 tree new_args = TI_ARGS (new_friend_template_info);
9857
9858 /* Overwrite whatever template info was there before, if
9859 any, with the new template information pertaining to
9860 the declaration. */
9861 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9862
9863 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9864 {
9865 /* We should have called reregister_specialization in
9866 duplicate_decls. */
9867 gcc_assert (retrieve_specialization (new_template,
9868 new_args, 0)
9869 == old_decl);
9870
9871 /* Instantiate it if the global has already been used. */
9872 if (DECL_ODR_USED (old_decl))
9873 instantiate_decl (old_decl, /*defer_ok=*/true,
9874 /*expl_inst_class_mem_p=*/false);
9875 }
9876 else
9877 {
9878 tree t;
9879
9880 /* Indicate that the old function template is a partial
9881 instantiation. */
9882 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9883 = new_friend_result_template_info;
9884
9885 gcc_assert (new_template
9886 == most_general_template (new_template));
9887 gcc_assert (new_template != old_decl);
9888
9889 /* Reassign any specializations already in the hash table
9890 to the new more general template, and add the
9891 additional template args. */
9892 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9893 t != NULL_TREE;
9894 t = TREE_CHAIN (t))
9895 {
9896 tree spec = TREE_VALUE (t);
9897 spec_entry elt;
9898
9899 elt.tmpl = old_decl;
9900 elt.args = DECL_TI_ARGS (spec);
9901 elt.spec = NULL_TREE;
9902
9903 decl_specializations->remove_elt (&elt);
9904
9905 DECL_TI_ARGS (spec)
9906 = add_outermost_template_args (new_args,
9907 DECL_TI_ARGS (spec));
9908
9909 register_specialization
9910 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9911
9912 }
9913 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9914 }
9915 }
9916
9917 /* The information from NEW_FRIEND has been merged into OLD_DECL
9918 by duplicate_decls. */
9919 new_friend = old_decl;
9920 }
9921 }
9922 else
9923 {
9924 tree context = DECL_CONTEXT (new_friend);
9925 bool dependent_p;
9926
9927 /* In the code
9928 template <class T> class C {
9929 template <class U> friend void C1<U>::f (); // case 1
9930 friend void C2<T>::f (); // case 2
9931 };
9932 we only need to make sure CONTEXT is a complete type for
9933 case 2. To distinguish between the two cases, we note that
9934 CONTEXT of case 1 remains dependent type after tsubst while
9935 this isn't true for case 2. */
9936 ++processing_template_decl;
9937 dependent_p = dependent_type_p (context);
9938 --processing_template_decl;
9939
9940 if (!dependent_p
9941 && !complete_type_or_else (context, NULL_TREE))
9942 return error_mark_node;
9943
9944 if (COMPLETE_TYPE_P (context))
9945 {
9946 tree fn = new_friend;
9947 /* do_friend adds the TEMPLATE_DECL for any member friend
9948 template even if it isn't a member template, i.e.
9949 template <class T> friend A<T>::f();
9950 Look through it in that case. */
9951 if (TREE_CODE (fn) == TEMPLATE_DECL
9952 && !PRIMARY_TEMPLATE_P (fn))
9953 fn = DECL_TEMPLATE_RESULT (fn);
9954 /* Check to see that the declaration is really present, and,
9955 possibly obtain an improved declaration. */
9956 fn = check_classfn (context, fn, NULL_TREE);
9957
9958 if (fn)
9959 new_friend = fn;
9960 }
9961 }
9962
9963 return new_friend;
9964}
9965
9966/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9967 template arguments, as for tsubst.
9968
9969 Returns an appropriate tsubst'd friend type or error_mark_node on
9970 failure. */
9971
9972static tree
9973tsubst_friend_class (tree friend_tmpl, tree args)
9974{
9975 tree friend_type;
9976 tree tmpl;
9977 tree context;
9978
9979 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9980 {
9981 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9982 return TREE_TYPE (t);
9983 }
9984
9985 context = CP_DECL_CONTEXT (friend_tmpl);
9986
9987 if (context != global_namespace)
9988 {
9989 if (TREE_CODE (context) == NAMESPACE_DECL)
9990 push_nested_namespace (context);
9991 else
9992 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9993 }
9994
9995 /* Look for a class template declaration. We look for hidden names
9996 because two friend declarations of the same template are the
9997 same. For example, in:
9998
9999 struct A {
10000 template <typename> friend class F;
10001 };
10002 template <typename> struct B {
10003 template <typename> friend class F;
10004 };
10005
10006 both F templates are the same. */
10007 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10008 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10009
10010 /* But, if we don't find one, it might be because we're in a
10011 situation like this:
10012
10013 template <class T>
10014 struct S {
10015 template <class U>
10016 friend struct S;
10017 };
10018
10019 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10020 for `S<int>', not the TEMPLATE_DECL. */
10021 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10022 {
10023 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10024 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10025 }
10026
10027 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10028 {
10029 /* The friend template has already been declared. Just
10030 check to see that the declarations match, and install any new
10031 default parameters. We must tsubst the default parameters,
10032 of course. We only need the innermost template parameters
10033 because that is all that redeclare_class_template will look
10034 at. */
10035 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10036 > TMPL_ARGS_DEPTH (args))
10037 {
10038 tree parms;
10039 location_t saved_input_location;
10040 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10041 args, tf_warning_or_error);
10042
10043 saved_input_location = input_location;
10044 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10045 tree cons = get_constraints (tmpl);
10046 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10047 input_location = saved_input_location;
10048
10049 }
10050
10051 friend_type = TREE_TYPE (tmpl);
10052 }
10053 else
10054 {
10055 /* The friend template has not already been declared. In this
10056 case, the instantiation of the template class will cause the
10057 injection of this template into the global scope. */
10058 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10059 if (tmpl == error_mark_node)
10060 return error_mark_node;
10061
10062 /* The new TMPL is not an instantiation of anything, so we
10063 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10064 the new type because that is supposed to be the corresponding
10065 template decl, i.e., TMPL. */
10066 DECL_USE_TEMPLATE (tmpl) = 0;
10067 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10068 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10069 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10070 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10071
10072 /* Inject this template into the global scope. */
10073 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
10074 }
10075
10076 if (context != global_namespace)
10077 {
10078 if (TREE_CODE (context) == NAMESPACE_DECL)
10079 pop_nested_namespace (context);
10080 else
10081 pop_nested_class ();
10082 }
10083
10084 return friend_type;
10085}
10086
10087/* Returns zero if TYPE cannot be completed later due to circularity.
10088 Otherwise returns one. */
10089
10090static int
10091can_complete_type_without_circularity (tree type)
10092{
10093 if (type == NULL_TREE || type == error_mark_node)
10094 return 0;
10095 else if (COMPLETE_TYPE_P (type))
10096 return 1;
10097 else if (TREE_CODE (type) == ARRAY_TYPE)
10098 return can_complete_type_without_circularity (TREE_TYPE (type));
10099 else if (CLASS_TYPE_P (type)
10100 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10101 return 0;
10102 else
10103 return 1;
10104}
10105
10106static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10107 tsubst_flags_t, tree);
10108
10109/* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10110 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10111
10112static tree
10113tsubst_attribute (tree t, tree *decl_p, tree args,
10114 tsubst_flags_t complain, tree in_decl)
10115{
10116 gcc_assert (ATTR_IS_DEPENDENT (t));
10117
10118 tree val = TREE_VALUE (t);
10119 if (val == NULL_TREE)
10120 /* Nothing to do. */;
10121 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10122 && is_attribute_p ("omp declare simd",
10123 get_attribute_name (t)))
10124 {
10125 tree clauses = TREE_VALUE (val);
10126 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10127 complain, in_decl);
10128 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10129 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10130 tree parms = DECL_ARGUMENTS (*decl_p);
10131 clauses
10132 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10133 if (clauses)
10134 val = build_tree_list (NULL_TREE, clauses);
10135 else
10136 val = NULL_TREE;
10137 }
10138 /* If the first attribute argument is an identifier, don't
10139 pass it through tsubst. Attributes like mode, format,
10140 cleanup and several target specific attributes expect it
10141 unmodified. */
10142 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10143 {
10144 tree chain
10145 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10146 /*integral_constant_expression_p=*/false);
10147 if (chain != TREE_CHAIN (val))
10148 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10149 }
10150 else if (PACK_EXPANSION_P (val))
10151 {
10152 /* An attribute pack expansion. */
10153 tree purp = TREE_PURPOSE (t);
10154 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10155 if (pack == error_mark_node)
10156 return error_mark_node;
10157 int len = TREE_VEC_LENGTH (pack);
10158 tree list = NULL_TREE;
10159 tree *q = &list;
10160 for (int i = 0; i < len; ++i)
10161 {
10162 tree elt = TREE_VEC_ELT (pack, i);
10163 *q = build_tree_list (purp, elt);
10164 q = &TREE_CHAIN (*q);
10165 }
10166 return list;
10167 }
10168 else
10169 val = tsubst_expr (val, args, complain, in_decl,
10170 /*integral_constant_expression_p=*/false);
10171
10172 if (val != TREE_VALUE (t))
10173 return build_tree_list (TREE_PURPOSE (t), val);
10174 return t;
10175}
10176
10177/* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10178 unchanged or a new TREE_LIST chain. */
10179
10180static tree
10181tsubst_attributes (tree attributes, tree args,
10182 tsubst_flags_t complain, tree in_decl)
10183{
10184 tree last_dep = NULL_TREE;
10185
10186 for (tree t = attributes; t; t = TREE_CHAIN (t))
10187 if (ATTR_IS_DEPENDENT (t))
10188 {
10189 last_dep = t;
10190 attributes = copy_list (attributes);
10191 break;
10192 }
10193
10194 if (last_dep)
10195 for (tree *p = &attributes; *p; )
10196 {
10197 tree t = *p;
10198 if (ATTR_IS_DEPENDENT (t))
10199 {
10200 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10201 if (subst != t)
10202 {
10203 *p = subst;
10204 do
10205 p = &TREE_CHAIN (*p);
10206 while (*p);
10207 *p = TREE_CHAIN (t);
10208 continue;
10209 }
10210 }
10211 p = &TREE_CHAIN (*p);
10212 }
10213
10214 return attributes;
10215}
10216
10217/* Apply any attributes which had to be deferred until instantiation
10218 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10219 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10220
10221static void
10222apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10223 tree args, tsubst_flags_t complain, tree in_decl)
10224{
10225 tree last_dep = NULL_TREE;
10226 tree t;
10227 tree *p;
10228
10229 if (attributes == NULL_TREE)
10230 return;
10231
10232 if (DECL_P (*decl_p))
10233 {
10234 if (TREE_TYPE (*decl_p) == error_mark_node)
10235 return;
10236 p = &DECL_ATTRIBUTES (*decl_p);
10237 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10238 to our attributes parameter. */
10239 gcc_assert (*p == attributes);
10240 }
10241 else
10242 {
10243 p = &TYPE_ATTRIBUTES (*decl_p);
10244 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10245 lookup_template_class_1, and should be preserved. */
10246 gcc_assert (*p != attributes);
10247 while (*p)
10248 p = &TREE_CHAIN (*p);
10249 }
10250
10251 for (t = attributes; t; t = TREE_CHAIN (t))
10252 if (ATTR_IS_DEPENDENT (t))
10253 {
10254 last_dep = t;
10255 attributes = copy_list (attributes);
10256 break;
10257 }
10258
10259 *p = attributes;
10260 if (last_dep)
10261 {
10262 tree late_attrs = NULL_TREE;
10263 tree *q = &late_attrs;
10264
10265 for (; *p; )
10266 {
10267 t = *p;
10268 if (ATTR_IS_DEPENDENT (t))
10269 {
10270 *p = TREE_CHAIN (t);
10271 TREE_CHAIN (t) = NULL_TREE;
10272 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10273 do
10274 q = &TREE_CHAIN (*q);
10275 while (*q);
10276 }
10277 else
10278 p = &TREE_CHAIN (t);
10279 }
10280
10281 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10282 }
10283}
10284
10285/* Perform (or defer) access check for typedefs that were referenced
10286 from within the template TMPL code.
10287 This is a subroutine of instantiate_decl and instantiate_class_template.
10288 TMPL is the template to consider and TARGS is the list of arguments of
10289 that template. */
10290
10291static void
10292perform_typedefs_access_check (tree tmpl, tree targs)
10293{
10294 location_t saved_location;
10295 unsigned i;
10296 qualified_typedef_usage_t *iter;
10297
10298 if (!tmpl
10299 || (!CLASS_TYPE_P (tmpl)
10300 && TREE_CODE (tmpl) != FUNCTION_DECL))
10301 return;
10302
10303 saved_location = input_location;
10304 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10305 {
10306 tree type_decl = iter->typedef_decl;
10307 tree type_scope = iter->context;
10308
10309 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10310 continue;
10311
10312 if (uses_template_parms (type_decl))
10313 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10314 if (uses_template_parms (type_scope))
10315 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10316
10317 /* Make access check error messages point to the location
10318 of the use of the typedef. */
10319 input_location = iter->locus;
10320 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10321 type_decl, type_decl,
10322 tf_warning_or_error);
10323 }
10324 input_location = saved_location;
10325}
10326
10327static tree
10328instantiate_class_template_1 (tree type)
10329{
10330 tree templ, args, pattern, t, member;
10331 tree typedecl;
10332 tree pbinfo;
10333 tree base_list;
10334 unsigned int saved_maximum_field_alignment;
10335 tree fn_context;
10336
10337 if (type == error_mark_node)
10338 return error_mark_node;
10339
10340 if (COMPLETE_OR_OPEN_TYPE_P (type)
10341 || uses_template_parms (type))
10342 return type;
10343
10344 /* Figure out which template is being instantiated. */
10345 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10346 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10347
10348 /* Determine what specialization of the original template to
10349 instantiate. */
10350 t = most_specialized_partial_spec (type, tf_warning_or_error);
10351 if (t == error_mark_node)
10352 {
10353 TYPE_BEING_DEFINED (type) = 1;
10354 return error_mark_node;
10355 }
10356 else if (t)
10357 {
10358 /* This TYPE is actually an instantiation of a partial
10359 specialization. We replace the innermost set of ARGS with
10360 the arguments appropriate for substitution. For example,
10361 given:
10362
10363 template <class T> struct S {};
10364 template <class T> struct S<T*> {};
10365
10366 and supposing that we are instantiating S<int*>, ARGS will
10367 presently be {int*} -- but we need {int}. */
10368 pattern = TREE_TYPE (t);
10369 args = TREE_PURPOSE (t);
10370 }
10371 else
10372 {
10373 pattern = TREE_TYPE (templ);
10374 args = CLASSTYPE_TI_ARGS (type);
10375 }
10376
10377 /* If the template we're instantiating is incomplete, then clearly
10378 there's nothing we can do. */
10379 if (!COMPLETE_TYPE_P (pattern))
10380 return type;
10381
10382 /* If we've recursively instantiated too many templates, stop. */
10383 if (! push_tinst_level (type))
10384 return type;
10385
10386 /* Now we're really doing the instantiation. Mark the type as in
10387 the process of being defined. */
10388 TYPE_BEING_DEFINED (type) = 1;
10389
10390 /* We may be in the middle of deferred access check. Disable
10391 it now. */
10392 push_deferring_access_checks (dk_no_deferred);
10393
10394 int saved_unevaluated_operand = cp_unevaluated_operand;
10395 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10396
10397 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10398 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10399 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10400 fn_context = error_mark_node;
10401 if (!fn_context)
10402 push_to_top_level ();
10403 else
10404 {
10405 cp_unevaluated_operand = 0;
10406 c_inhibit_evaluation_warnings = 0;
10407 }
10408 /* Use #pragma pack from the template context. */
10409 saved_maximum_field_alignment = maximum_field_alignment;
10410 maximum_field_alignment = TYPE_PRECISION (pattern);
10411
10412 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10413
10414 /* Set the input location to the most specialized template definition.
10415 This is needed if tsubsting causes an error. */
10416 typedecl = TYPE_MAIN_DECL (pattern);
10417 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10418 DECL_SOURCE_LOCATION (typedecl);
10419
10420 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10421 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10422 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10423 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10424 if (ANON_AGGR_TYPE_P (pattern))
10425 SET_ANON_AGGR_TYPE_P (type);
10426 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10427 {
10428 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10429 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10430 /* Adjust visibility for template arguments. */
10431 determine_visibility (TYPE_MAIN_DECL (type));
10432 }
10433 if (CLASS_TYPE_P (type))
10434 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10435
10436 pbinfo = TYPE_BINFO (pattern);
10437
10438 /* We should never instantiate a nested class before its enclosing
10439 class; we need to look up the nested class by name before we can
10440 instantiate it, and that lookup should instantiate the enclosing
10441 class. */
10442 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10443 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10444
10445 base_list = NULL_TREE;
10446 if (BINFO_N_BASE_BINFOS (pbinfo))
10447 {
10448 tree pbase_binfo;
10449 tree pushed_scope;
10450 int i;
10451
10452 /* We must enter the scope containing the type, as that is where
10453 the accessibility of types named in dependent bases are
10454 looked up from. */
10455 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10456
10457 /* Substitute into each of the bases to determine the actual
10458 basetypes. */
10459 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10460 {
10461 tree base;
10462 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10463 tree expanded_bases = NULL_TREE;
10464 int idx, len = 1;
10465
10466 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10467 {
10468 expanded_bases =
10469 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10470 args, tf_error, NULL_TREE);
10471 if (expanded_bases == error_mark_node)
10472 continue;
10473
10474 len = TREE_VEC_LENGTH (expanded_bases);
10475 }
10476
10477 for (idx = 0; idx < len; idx++)
10478 {
10479 if (expanded_bases)
10480 /* Extract the already-expanded base class. */
10481 base = TREE_VEC_ELT (expanded_bases, idx);
10482 else
10483 /* Substitute to figure out the base class. */
10484 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10485 NULL_TREE);
10486
10487 if (base == error_mark_node)
10488 continue;
10489
10490 base_list = tree_cons (access, base, base_list);
10491 if (BINFO_VIRTUAL_P (pbase_binfo))
10492 TREE_TYPE (base_list) = integer_type_node;
10493 }
10494 }
10495
10496 /* The list is now in reverse order; correct that. */
10497 base_list = nreverse (base_list);
10498
10499 if (pushed_scope)
10500 pop_scope (pushed_scope);
10501 }
10502 /* Now call xref_basetypes to set up all the base-class
10503 information. */
10504 xref_basetypes (type, base_list);
10505
10506 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10507 (int) ATTR_FLAG_TYPE_IN_PLACE,
10508 args, tf_error, NULL_TREE);
10509 fixup_attribute_variants (type);
10510
10511 /* Now that our base classes are set up, enter the scope of the
10512 class, so that name lookups into base classes, etc. will work
10513 correctly. This is precisely analogous to what we do in
10514 begin_class_definition when defining an ordinary non-template
10515 class, except we also need to push the enclosing classes. */
10516 push_nested_class (type);
10517
10518 /* Now members are processed in the order of declaration. */
10519 for (member = CLASSTYPE_DECL_LIST (pattern);
10520 member; member = TREE_CHAIN (member))
10521 {
10522 tree t = TREE_VALUE (member);
10523
10524 if (TREE_PURPOSE (member))
10525 {
10526 if (TYPE_P (t))
10527 {
10528 /* Build new CLASSTYPE_NESTED_UTDS. */
10529
10530 tree newtag;
10531 bool class_template_p;
10532
10533 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10534 && TYPE_LANG_SPECIFIC (t)
10535 && CLASSTYPE_IS_TEMPLATE (t));
10536 /* If the member is a class template, then -- even after
10537 substitution -- there may be dependent types in the
10538 template argument list for the class. We increment
10539 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10540 that function will assume that no types are dependent
10541 when outside of a template. */
10542 if (class_template_p)
10543 ++processing_template_decl;
10544 newtag = tsubst (t, args, tf_error, NULL_TREE);
10545 if (class_template_p)
10546 --processing_template_decl;
10547 if (newtag == error_mark_node)
10548 continue;
10549
10550 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10551 {
10552 tree name = TYPE_IDENTIFIER (t);
10553
10554 if (class_template_p)
10555 /* Unfortunately, lookup_template_class sets
10556 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10557 instantiation (i.e., for the type of a member
10558 template class nested within a template class.)
10559 This behavior is required for
10560 maybe_process_partial_specialization to work
10561 correctly, but is not accurate in this case;
10562 the TAG is not an instantiation of anything.
10563 (The corresponding TEMPLATE_DECL is an
10564 instantiation, but the TYPE is not.) */
10565 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10566
10567 /* Now, we call pushtag to put this NEWTAG into the scope of
10568 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10569 pushtag calling push_template_decl. We don't have to do
10570 this for enums because it will already have been done in
10571 tsubst_enum. */
10572 if (name)
10573 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10574 pushtag (name, newtag, /*tag_scope=*/ts_current);
10575 }
10576 }
10577 else if (DECL_DECLARES_FUNCTION_P (t))
10578 {
10579 /* Build new TYPE_METHODS. */
10580 tree r;
10581
10582 if (TREE_CODE (t) == TEMPLATE_DECL)
10583 ++processing_template_decl;
10584 r = tsubst (t, args, tf_error, NULL_TREE);
10585 if (TREE_CODE (t) == TEMPLATE_DECL)
10586 --processing_template_decl;
10587 set_current_access_from_decl (r);
10588 finish_member_declaration (r);
10589 /* Instantiate members marked with attribute used. */
10590 if (r != error_mark_node && DECL_PRESERVE_P (r))
10591 mark_used (r);
10592 if (TREE_CODE (r) == FUNCTION_DECL
10593 && DECL_OMP_DECLARE_REDUCTION_P (r))
10594 cp_check_omp_declare_reduction (r);
10595 }
10596 else if (DECL_CLASS_TEMPLATE_P (t)
10597 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10598 /* A closure type for a lambda in a default argument for a
10599 member template. Ignore it; it will be instantiated with
10600 the default argument. */;
10601 else
10602 {
10603 /* Build new TYPE_FIELDS. */
10604 if (TREE_CODE (t) == STATIC_ASSERT)
10605 {
10606 tree condition;
10607
10608 ++c_inhibit_evaluation_warnings;
10609 condition =
10610 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10611 tf_warning_or_error, NULL_TREE,
10612 /*integral_constant_expression_p=*/true);
10613 --c_inhibit_evaluation_warnings;
10614
10615 finish_static_assert (condition,
10616 STATIC_ASSERT_MESSAGE (t),
10617 STATIC_ASSERT_SOURCE_LOCATION (t),
10618 /*member_p=*/true);
10619 }
10620 else if (TREE_CODE (t) != CONST_DECL)
10621 {
10622 tree r;
10623 tree vec = NULL_TREE;
10624 int len = 1;
10625
10626 /* The file and line for this declaration, to
10627 assist in error message reporting. Since we
10628 called push_tinst_level above, we don't need to
10629 restore these. */
10630 input_location = DECL_SOURCE_LOCATION (t);
10631
10632 if (TREE_CODE (t) == TEMPLATE_DECL)
10633 ++processing_template_decl;
10634 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10635 if (TREE_CODE (t) == TEMPLATE_DECL)
10636 --processing_template_decl;
10637
10638 if (TREE_CODE (r) == TREE_VEC)
10639 {
10640 /* A capture pack became multiple fields. */
10641 vec = r;
10642 len = TREE_VEC_LENGTH (vec);
10643 }
10644
10645 for (int i = 0; i < len; ++i)
10646 {
10647 if (vec)
10648 r = TREE_VEC_ELT (vec, i);
10649 if (VAR_P (r))
10650 {
10651 /* In [temp.inst]:
10652
10653 [t]he initialization (and any associated
10654 side-effects) of a static data member does
10655 not occur unless the static data member is
10656 itself used in a way that requires the
10657 definition of the static data member to
10658 exist.
10659
10660 Therefore, we do not substitute into the
10661 initialized for the static data member here. */
10662 finish_static_data_member_decl
10663 (r,
10664 /*init=*/NULL_TREE,
10665 /*init_const_expr_p=*/false,
10666 /*asmspec_tree=*/NULL_TREE,
10667 /*flags=*/0);
10668 /* Instantiate members marked with attribute used. */
10669 if (r != error_mark_node && DECL_PRESERVE_P (r))
10670 mark_used (r);
10671 }
10672 else if (TREE_CODE (r) == FIELD_DECL)
10673 {
10674 /* Determine whether R has a valid type and can be
10675 completed later. If R is invalid, then its type
10676 is replaced by error_mark_node. */
10677 tree rtype = TREE_TYPE (r);
10678 if (can_complete_type_without_circularity (rtype))
10679 complete_type (rtype);
10680
10681 if (!complete_or_array_type_p (rtype))
10682 {
10683 /* If R's type couldn't be completed and
10684 it isn't a flexible array member (whose
10685 type is incomplete by definition) give
10686 an error. */
10687 cxx_incomplete_type_error (r, rtype);
10688 TREE_TYPE (r) = error_mark_node;
10689 }
10690 }
10691
10692 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10693 such a thing will already have been added to the field
10694 list by tsubst_enum in finish_member_declaration in the
10695 CLASSTYPE_NESTED_UTDS case above. */
10696 if (!(TREE_CODE (r) == TYPE_DECL
10697 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10698 && DECL_ARTIFICIAL (r)))
10699 {
10700 set_current_access_from_decl (r);
10701 finish_member_declaration (r);
10702 }
10703 }
10704 }
10705 }
10706 }
10707 else
10708 {
10709 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10710 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10711 {
10712 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10713
10714 tree friend_type = t;
10715 bool adjust_processing_template_decl = false;
10716
10717 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10718 {
10719 /* template <class T> friend class C; */
10720 friend_type = tsubst_friend_class (friend_type, args);
10721 adjust_processing_template_decl = true;
10722 }
10723 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10724 {
10725 /* template <class T> friend class C::D; */
10726 friend_type = tsubst (friend_type, args,
10727 tf_warning_or_error, NULL_TREE);
10728 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10729 friend_type = TREE_TYPE (friend_type);
10730 adjust_processing_template_decl = true;
10731 }
10732 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10733 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10734 {
10735 /* This could be either
10736
10737 friend class T::C;
10738
10739 when dependent_type_p is false or
10740
10741 template <class U> friend class T::C;
10742
10743 otherwise. */
10744 /* Bump processing_template_decl in case this is something like
10745 template <class T> friend struct A<T>::B. */
10746 ++processing_template_decl;
10747 friend_type = tsubst (friend_type, args,
10748 tf_warning_or_error, NULL_TREE);
10749 if (dependent_type_p (friend_type))
10750 adjust_processing_template_decl = true;
10751 --processing_template_decl;
10752 }
10753 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10754 && hidden_name_p (TYPE_NAME (friend_type)))
10755 {
10756 /* friend class C;
10757
10758 where C hasn't been declared yet. Let's lookup name
10759 from namespace scope directly, bypassing any name that
10760 come from dependent base class. */
10761 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10762
10763 /* The call to xref_tag_from_type does injection for friend
10764 classes. */
10765 push_nested_namespace (ns);
10766 friend_type =
10767 xref_tag_from_type (friend_type, NULL_TREE,
10768 /*tag_scope=*/ts_current);
10769 pop_nested_namespace (ns);
10770 }
10771 else if (uses_template_parms (friend_type))
10772 /* friend class C<T>; */
10773 friend_type = tsubst (friend_type, args,
10774 tf_warning_or_error, NULL_TREE);
10775 /* Otherwise it's
10776
10777 friend class C;
10778
10779 where C is already declared or
10780
10781 friend class C<int>;
10782
10783 We don't have to do anything in these cases. */
10784
10785 if (adjust_processing_template_decl)
10786 /* Trick make_friend_class into realizing that the friend
10787 we're adding is a template, not an ordinary class. It's
10788 important that we use make_friend_class since it will
10789 perform some error-checking and output cross-reference
10790 information. */
10791 ++processing_template_decl;
10792
10793 if (friend_type != error_mark_node)
10794 make_friend_class (type, friend_type, /*complain=*/false);
10795
10796 if (adjust_processing_template_decl)
10797 --processing_template_decl;
10798 }
10799 else
10800 {
10801 /* Build new DECL_FRIENDLIST. */
10802 tree r;
10803
10804 /* The file and line for this declaration, to
10805 assist in error message reporting. Since we
10806 called push_tinst_level above, we don't need to
10807 restore these. */
10808 input_location = DECL_SOURCE_LOCATION (t);
10809
10810 if (TREE_CODE (t) == TEMPLATE_DECL)
10811 {
10812 ++processing_template_decl;
10813 push_deferring_access_checks (dk_no_check);
10814 }
10815
10816 r = tsubst_friend_function (t, args);
10817 add_friend (type, r, /*complain=*/false);
10818 if (TREE_CODE (t) == TEMPLATE_DECL)
10819 {
10820 pop_deferring_access_checks ();
10821 --processing_template_decl;
10822 }
10823 }
10824 }
10825 }
10826
10827 if (fn_context)
10828 {
10829 /* Restore these before substituting into the lambda capture
10830 initializers. */
10831 cp_unevaluated_operand = saved_unevaluated_operand;
10832 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10833 }
10834
10835 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10836 {
10837 tree decl = lambda_function (type);
10838 if (decl)
10839 {
10840 if (cxx_dialect >= cxx1z)
10841 CLASSTYPE_LITERAL_P (type) = true;
10842
10843 if (!DECL_TEMPLATE_INFO (decl)
10844 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10845 {
10846 /* Set function_depth to avoid garbage collection. */
10847 ++function_depth;
10848 instantiate_decl (decl, /*defer_ok=*/false, false);
10849 --function_depth;
10850 }
10851
10852 /* We need to instantiate the capture list from the template
10853 after we've instantiated the closure members, but before we
10854 consider adding the conversion op. Also keep any captures
10855 that may have been added during instantiation of the op(). */
10856 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10857 tree tmpl_cap
10858 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10859 args, tf_warning_or_error, NULL_TREE,
10860 false, false);
10861
10862 LAMBDA_EXPR_CAPTURE_LIST (expr)
10863 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10864
10865 maybe_add_lambda_conv_op (type);
10866 }
10867 else
10868 gcc_assert (errorcount);
10869 }
10870
10871 /* Set the file and line number information to whatever is given for
10872 the class itself. This puts error messages involving generated
10873 implicit functions at a predictable point, and the same point
10874 that would be used for non-template classes. */
10875 input_location = DECL_SOURCE_LOCATION (typedecl);
10876
10877 unreverse_member_declarations (type);
10878 finish_struct_1 (type);
10879 TYPE_BEING_DEFINED (type) = 0;
10880
10881 /* We don't instantiate default arguments for member functions. 14.7.1:
10882
10883 The implicit instantiation of a class template specialization causes
10884 the implicit instantiation of the declarations, but not of the
10885 definitions or default arguments, of the class member functions,
10886 member classes, static data members and member templates.... */
10887
10888 /* Some typedefs referenced from within the template code need to be access
10889 checked at template instantiation time, i.e now. These types were
10890 added to the template at parsing time. Let's get those and perform
10891 the access checks then. */
10892 perform_typedefs_access_check (pattern, args);
10893 perform_deferred_access_checks (tf_warning_or_error);
10894 pop_nested_class ();
10895 maximum_field_alignment = saved_maximum_field_alignment;
10896 if (!fn_context)
10897 pop_from_top_level ();
10898 pop_deferring_access_checks ();
10899 pop_tinst_level ();
10900
10901 /* The vtable for a template class can be emitted in any translation
10902 unit in which the class is instantiated. When there is no key
10903 method, however, finish_struct_1 will already have added TYPE to
10904 the keyed_classes list. */
10905 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10906 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10907
10908 return type;
10909}
10910
10911/* Wrapper for instantiate_class_template_1. */
10912
10913tree
10914instantiate_class_template (tree type)
10915{
10916 tree ret;
10917 timevar_push (TV_TEMPLATE_INST);
10918 ret = instantiate_class_template_1 (type);
10919 timevar_pop (TV_TEMPLATE_INST);
10920 return ret;
10921}
10922
10923static tree
10924tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10925{
10926 tree r;
10927
10928 if (!t)
10929 r = t;
10930 else if (TYPE_P (t))
10931 r = tsubst (t, args, complain, in_decl);
10932 else
10933 {
10934 if (!(complain & tf_warning))
10935 ++c_inhibit_evaluation_warnings;
10936 r = tsubst_expr (t, args, complain, in_decl,
10937 /*integral_constant_expression_p=*/true);
10938 if (!(complain & tf_warning))
10939 --c_inhibit_evaluation_warnings;
10940 }
10941 return r;
10942}
10943
10944/* Given a function parameter pack TMPL_PARM and some function parameters
10945 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10946 and set *SPEC_P to point at the next point in the list. */
10947
10948tree
10949extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10950{
10951 /* Collect all of the extra "packed" parameters into an
10952 argument pack. */
10953 tree parmvec;
10954 tree parmtypevec;
10955 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10956 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10957 tree spec_parm = *spec_p;
10958 int i, len;
10959
10960 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10961 if (tmpl_parm
10962 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10963 break;
10964
10965 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10966 parmvec = make_tree_vec (len);
10967 parmtypevec = make_tree_vec (len);
10968 spec_parm = *spec_p;
10969 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10970 {
10971 TREE_VEC_ELT (parmvec, i) = spec_parm;
10972 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10973 }
10974
10975 /* Build the argument packs. */
10976 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10977 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10978 TREE_TYPE (argpack) = argtypepack;
10979 *spec_p = spec_parm;
10980
10981 return argpack;
10982}
10983
10984/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10985 NONTYPE_ARGUMENT_PACK. */
10986
10987static tree
10988make_fnparm_pack (tree spec_parm)
10989{
10990 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10991}
10992
10993/* Return 1 if the Ith element of the argument pack ARG_PACK is a
10994 pack expansion with no extra args, 2 if it has extra args, or 0
10995 if it is not a pack expansion. */
10996
10997static int
10998argument_pack_element_is_expansion_p (tree arg_pack, int i)
10999{
11000 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11001 if (i >= TREE_VEC_LENGTH (vec))
11002 return 0;
11003 tree elt = TREE_VEC_ELT (vec, i);
11004 if (DECL_P (elt))
11005 /* A decl pack is itself an expansion. */
11006 elt = TREE_TYPE (elt);
11007 if (!PACK_EXPANSION_P (elt))
11008 return 0;
11009 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11010 return 2;
11011 return 1;
11012}
11013
11014
11015/* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11016
11017static tree
11018make_argument_pack_select (tree arg_pack, unsigned index)
11019{
11020 tree aps = make_node (ARGUMENT_PACK_SELECT);
11021
11022 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11023 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11024
11025 return aps;
11026}
11027
11028/* This is a subroutine of tsubst_pack_expansion.
11029
11030 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11031 mechanism to store the (non complete list of) arguments of the
11032 substitution and return a non substituted pack expansion, in order
11033 to wait for when we have enough arguments to really perform the
11034 substitution. */
11035
11036static bool
11037use_pack_expansion_extra_args_p (tree parm_packs,
11038 int arg_pack_len,
11039 bool has_empty_arg)
11040{
11041 /* If one pack has an expansion and another pack has a normal
11042 argument or if one pack has an empty argument and an another
11043 one hasn't then tsubst_pack_expansion cannot perform the
11044 substitution and need to fall back on the
11045 PACK_EXPANSION_EXTRA mechanism. */
11046 if (parm_packs == NULL_TREE)
11047 return false;
11048 else if (has_empty_arg)
11049 return true;
11050
11051 bool has_expansion_arg = false;
11052 for (int i = 0 ; i < arg_pack_len; ++i)
11053 {
11054 bool has_non_expansion_arg = false;
11055 for (tree parm_pack = parm_packs;
11056 parm_pack;
11057 parm_pack = TREE_CHAIN (parm_pack))
11058 {
11059 tree arg = TREE_VALUE (parm_pack);
11060
11061 int exp = argument_pack_element_is_expansion_p (arg, i);
11062 if (exp == 2)
11063 /* We can't substitute a pack expansion with extra args into
11064 our pattern. */
11065 return true;
11066 else if (exp)
11067 has_expansion_arg = true;
11068 else
11069 has_non_expansion_arg = true;
11070 }
11071
11072 if (has_expansion_arg && has_non_expansion_arg)
11073 return true;
11074 }
11075 return false;
11076}
11077
11078/* [temp.variadic]/6 says that:
11079
11080 The instantiation of a pack expansion [...]
11081 produces a list E1,E2, ..., En, where N is the number of elements
11082 in the pack expansion parameters.
11083
11084 This subroutine of tsubst_pack_expansion produces one of these Ei.
11085
11086 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11087 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11088 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11089 INDEX is the index 'i' of the element Ei to produce. ARGS,
11090 COMPLAIN, and IN_DECL are the same parameters as for the
11091 tsubst_pack_expansion function.
11092
11093 The function returns the resulting Ei upon successful completion,
11094 or error_mark_node.
11095
11096 Note that this function possibly modifies the ARGS parameter, so
11097 it's the responsibility of the caller to restore it. */
11098
11099static tree
11100gen_elem_of_pack_expansion_instantiation (tree pattern,
11101 tree parm_packs,
11102 unsigned index,
11103 tree args /* This parm gets
11104 modified. */,
11105 tsubst_flags_t complain,
11106 tree in_decl)
11107{
11108 tree t;
11109 bool ith_elem_is_expansion = false;
11110
11111 /* For each parameter pack, change the substitution of the parameter
11112 pack to the ith argument in its argument pack, then expand the
11113 pattern. */
11114 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11115 {
11116 tree parm = TREE_PURPOSE (pack);
11117 tree arg_pack = TREE_VALUE (pack);
11118 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11119
11120 ith_elem_is_expansion |=
11121 argument_pack_element_is_expansion_p (arg_pack, index);
11122
11123 /* Select the Ith argument from the pack. */
11124 if (TREE_CODE (parm) == PARM_DECL
11125 || TREE_CODE (parm) == FIELD_DECL)
11126 {
11127 if (index == 0)
11128 {
11129 aps = make_argument_pack_select (arg_pack, index);
11130 if (!mark_used (parm, complain) && !(complain & tf_error))
11131 return error_mark_node;
11132 register_local_specialization (aps, parm);
11133 }
11134 else
11135 aps = retrieve_local_specialization (parm);
11136 }
11137 else
11138 {
11139 int idx, level;
11140 template_parm_level_and_index (parm, &level, &idx);
11141
11142 if (index == 0)
11143 {
11144 aps = make_argument_pack_select (arg_pack, index);
11145 /* Update the corresponding argument. */
11146 TMPL_ARG (args, level, idx) = aps;
11147 }
11148 else
11149 /* Re-use the ARGUMENT_PACK_SELECT. */
11150 aps = TMPL_ARG (args, level, idx);
11151 }
11152 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11153 }
11154
11155 /* Substitute into the PATTERN with the (possibly altered)
11156 arguments. */
11157 if (pattern == in_decl)
11158 /* Expanding a fixed parameter pack from
11159 coerce_template_parameter_pack. */
11160 t = tsubst_decl (pattern, args, complain);
11161 else if (pattern == error_mark_node)
11162 t = error_mark_node;
11163 else if (constraint_p (pattern))
11164 {
11165 if (processing_template_decl)
11166 t = tsubst_constraint (pattern, args, complain, in_decl);
11167 else
11168 t = (constraints_satisfied_p (pattern, args)
11169 ? boolean_true_node : boolean_false_node);
11170 }
11171 else if (!TYPE_P (pattern))
11172 t = tsubst_expr (pattern, args, complain, in_decl,
11173 /*integral_constant_expression_p=*/false);
11174 else
11175 t = tsubst (pattern, args, complain, in_decl);
11176
11177 /* If the Ith argument pack element is a pack expansion, then
11178 the Ith element resulting from the substituting is going to
11179 be a pack expansion as well. */
11180 if (ith_elem_is_expansion)
11181 t = make_pack_expansion (t);
11182
11183 return t;
11184}
11185
11186/* When the unexpanded parameter pack in a fold expression expands to an empty
11187 sequence, the value of the expression is as follows; the program is
11188 ill-formed if the operator is not listed in this table.
11189
11190 && true
11191 || false
11192 , void() */
11193
11194tree
11195expand_empty_fold (tree t, tsubst_flags_t complain)
11196{
11197 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11198 if (!FOLD_EXPR_MODIFY_P (t))
11199 switch (code)
11200 {
11201 case TRUTH_ANDIF_EXPR:
11202 return boolean_true_node;
11203 case TRUTH_ORIF_EXPR:
11204 return boolean_false_node;
11205 case COMPOUND_EXPR:
11206 return void_node;
11207 default:
11208 break;
11209 }
11210
11211 if (complain & tf_error)
11212 error_at (location_of (t),
11213 "fold of empty expansion over %O", code);
11214 return error_mark_node;
11215}
11216
11217/* Given a fold-expression T and a current LEFT and RIGHT operand,
11218 form an expression that combines the two terms using the
11219 operator of T. */
11220
11221static tree
11222fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11223{
11224 tree op = FOLD_EXPR_OP (t);
11225 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11226
11227 // Handle compound assignment operators.
11228 if (FOLD_EXPR_MODIFY_P (t))
11229 return build_x_modify_expr (input_location, left, code, right, complain);
11230
11231 switch (code)
11232 {
11233 case COMPOUND_EXPR:
11234 return build_x_compound_expr (input_location, left, right, complain);
11235 case DOTSTAR_EXPR:
11236 return build_m_component_ref (left, right, complain);
11237 default:
11238 return build_x_binary_op (input_location, code,
11239 left, TREE_CODE (left),
11240 right, TREE_CODE (right),
11241 /*overload=*/NULL,
11242 complain);
11243 }
11244}
11245
11246/* Substitute ARGS into the pack of a fold expression T. */
11247
11248static inline tree
11249tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11250{
11251 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11252}
11253
11254/* Substitute ARGS into the pack of a fold expression T. */
11255
11256static inline tree
11257tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11258{
11259 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11260}
11261
11262/* Expand a PACK of arguments into a grouped as left fold.
11263 Given a pack containing elements A0, A1, ..., An and an
11264 operator @, this builds the expression:
11265
11266 ((A0 @ A1) @ A2) ... @ An
11267
11268 Note that PACK must not be empty.
11269
11270 The operator is defined by the original fold expression T. */
11271
11272static tree
11273expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11274{
11275 tree left = TREE_VEC_ELT (pack, 0);
11276 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11277 {
11278 tree right = TREE_VEC_ELT (pack, i);
11279 left = fold_expression (t, left, right, complain);
11280 }
11281 return left;
11282}
11283
11284/* Substitute into a unary left fold expression. */
11285
11286static tree
11287tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11288 tree in_decl)
11289{
11290 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11291 if (pack == error_mark_node)
11292 return error_mark_node;
11293 if (PACK_EXPANSION_P (pack))
11294 {
11295 tree r = copy_node (t);
11296 FOLD_EXPR_PACK (r) = pack;
11297 return r;
11298 }
11299 if (TREE_VEC_LENGTH (pack) == 0)
11300 return expand_empty_fold (t, complain);
11301 else
11302 return expand_left_fold (t, pack, complain);
11303}
11304
11305/* Substitute into a binary left fold expression.
11306
11307 Do ths by building a single (non-empty) vector of argumnts and
11308 building the expression from those elements. */
11309
11310static tree
11311tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11312 tree in_decl)
11313{
11314 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11315 if (pack == error_mark_node)
11316 return error_mark_node;
11317 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11318 if (init == error_mark_node)
11319 return error_mark_node;
11320
11321 if (PACK_EXPANSION_P (pack))
11322 {
11323 tree r = copy_node (t);
11324 FOLD_EXPR_PACK (r) = pack;
11325 FOLD_EXPR_INIT (r) = init;
11326 return r;
11327 }
11328
11329 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11330 TREE_VEC_ELT (vec, 0) = init;
11331 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11332 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11333
11334 return expand_left_fold (t, vec, complain);
11335}
11336
11337/* Expand a PACK of arguments into a grouped as right fold.
11338 Given a pack containing elementns A0, A1, ..., and an
11339 operator @, this builds the expression:
11340
11341 A0@ ... (An-2 @ (An-1 @ An))
11342
11343 Note that PACK must not be empty.
11344
11345 The operator is defined by the original fold expression T. */
11346
11347tree
11348expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11349{
11350 // Build the expression.
11351 int n = TREE_VEC_LENGTH (pack);
11352 tree right = TREE_VEC_ELT (pack, n - 1);
11353 for (--n; n != 0; --n)
11354 {
11355 tree left = TREE_VEC_ELT (pack, n - 1);
11356 right = fold_expression (t, left, right, complain);
11357 }
11358 return right;
11359}
11360
11361/* Substitute into a unary right fold expression. */
11362
11363static tree
11364tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11365 tree in_decl)
11366{
11367 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11368 if (pack == error_mark_node)
11369 return error_mark_node;
11370 if (PACK_EXPANSION_P (pack))
11371 {
11372 tree r = copy_node (t);
11373 FOLD_EXPR_PACK (r) = pack;
11374 return r;
11375 }
11376 if (TREE_VEC_LENGTH (pack) == 0)
11377 return expand_empty_fold (t, complain);
11378 else
11379 return expand_right_fold (t, pack, complain);
11380}
11381
11382/* Substitute into a binary right fold expression.
11383
11384 Do ths by building a single (non-empty) vector of arguments and
11385 building the expression from those elements. */
11386
11387static tree
11388tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11389 tree in_decl)
11390{
11391 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11392 if (pack == error_mark_node)
11393 return error_mark_node;
11394 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11395 if (init == error_mark_node)
11396 return error_mark_node;
11397
11398 if (PACK_EXPANSION_P (pack))
11399 {
11400 tree r = copy_node (t);
11401 FOLD_EXPR_PACK (r) = pack;
11402 FOLD_EXPR_INIT (r) = init;
11403 return r;
11404 }
11405
11406 int n = TREE_VEC_LENGTH (pack);
11407 tree vec = make_tree_vec (n + 1);
11408 for (int i = 0; i < n; ++i)
11409 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11410 TREE_VEC_ELT (vec, n) = init;
11411
11412 return expand_right_fold (t, vec, complain);
11413}
11414
11415
11416/* Substitute ARGS into T, which is an pack expansion
11417 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11418 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11419 (if only a partial substitution could be performed) or
11420 ERROR_MARK_NODE if there was an error. */
11421tree
11422tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11423 tree in_decl)
11424{
11425 tree pattern;
11426 tree pack, packs = NULL_TREE;
11427 bool unsubstituted_packs = false;
11428 int i, len = -1;
11429 tree result;
11430 hash_map<tree, tree> *saved_local_specializations = NULL;
11431 bool need_local_specializations = false;
11432 int levels;
11433
11434 gcc_assert (PACK_EXPANSION_P (t));
11435 pattern = PACK_EXPANSION_PATTERN (t);
11436
11437 /* Add in any args remembered from an earlier partial instantiation. */
11438 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11439
11440 levels = TMPL_ARGS_DEPTH (args);
11441
11442 /* Determine the argument packs that will instantiate the parameter
11443 packs used in the expansion expression. While we're at it,
11444 compute the number of arguments to be expanded and make sure it
11445 is consistent. */
11446 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11447 pack = TREE_CHAIN (pack))
11448 {
11449 tree parm_pack = TREE_VALUE (pack);
11450 tree arg_pack = NULL_TREE;
11451 tree orig_arg = NULL_TREE;
11452 int level = 0;
11453
11454 if (TREE_CODE (parm_pack) == BASES)
11455 {
11456 if (BASES_DIRECT (parm_pack))
11457 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11458 args, complain, in_decl, false));
11459 else
11460 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11461 args, complain, in_decl, false));
11462 }
11463 if (TREE_CODE (parm_pack) == PARM_DECL)
11464 {
11465 /* We know we have correct local_specializations if this
11466 expansion is at function scope, or if we're dealing with a
11467 local parameter in a requires expression; for the latter,
11468 tsubst_requires_expr set it up appropriately. */
11469 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11470 arg_pack = retrieve_local_specialization (parm_pack);
11471 else
11472 /* We can't rely on local_specializations for a parameter
11473 name used later in a function declaration (such as in a
11474 late-specified return type). Even if it exists, it might
11475 have the wrong value for a recursive call. */
11476 need_local_specializations = true;
11477
11478 if (!arg_pack)
11479 {
11480 /* This parameter pack was used in an unevaluated context. Just
11481 make a dummy decl, since it's only used for its type. */
11482 ++cp_unevaluated_operand;
11483 arg_pack = tsubst_decl (parm_pack, args, complain);
11484 --cp_unevaluated_operand;
11485 if (arg_pack && DECL_PACK_P (arg_pack))
11486 /* Partial instantiation of the parm_pack, we can't build
11487 up an argument pack yet. */
11488 arg_pack = NULL_TREE;
11489 else
11490 arg_pack = make_fnparm_pack (arg_pack);
11491 }
11492 }
11493 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11494 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11495 else
11496 {
11497 int idx;
11498 template_parm_level_and_index (parm_pack, &level, &idx);
11499
11500 if (level <= levels)
11501 arg_pack = TMPL_ARG (args, level, idx);
11502 }
11503
11504 orig_arg = arg_pack;
11505 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11506 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11507
11508 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11509 /* This can only happen if we forget to expand an argument
11510 pack somewhere else. Just return an error, silently. */
11511 {
11512 result = make_tree_vec (1);
11513 TREE_VEC_ELT (result, 0) = error_mark_node;
11514 return result;
11515 }
11516
11517 if (arg_pack)
11518 {
11519 int my_len =
11520 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11521
11522 /* Don't bother trying to do a partial substitution with
11523 incomplete packs; we'll try again after deduction. */
11524 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11525 return t;
11526
11527 if (len < 0)
11528 len = my_len;
11529 else if (len != my_len)
11530 {
11531 if (!(complain & tf_error))
11532 /* Fail quietly. */;
11533 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11534 error ("mismatched argument pack lengths while expanding "
11535 "%<%T%>",
11536 pattern);
11537 else
11538 error ("mismatched argument pack lengths while expanding "
11539 "%<%E%>",
11540 pattern);
11541 return error_mark_node;
11542 }
11543
11544 /* Keep track of the parameter packs and their corresponding
11545 argument packs. */
11546 packs = tree_cons (parm_pack, arg_pack, packs);
11547 TREE_TYPE (packs) = orig_arg;
11548 }
11549 else
11550 {
11551 /* We can't substitute for this parameter pack. We use a flag as
11552 well as the missing_level counter because function parameter
11553 packs don't have a level. */
11554 gcc_assert (processing_template_decl || is_auto (parm_pack));
11555 unsubstituted_packs = true;
11556 }
11557 }
11558
11559 /* If the expansion is just T..., return the matching argument pack, unless
11560 we need to call convert_from_reference on all the elements. This is an
11561 important optimization; see c++/68422. */
11562 if (!unsubstituted_packs
11563 && TREE_PURPOSE (packs) == pattern)
11564 {
11565 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11566 /* Types need no adjustment, nor does sizeof..., and if we still have
11567 some pack expansion args we won't do anything yet. */
11568 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11569 || PACK_EXPANSION_SIZEOF_P (t)
11570 || pack_expansion_args_count (args))
11571 return args;
11572 /* Also optimize expression pack expansions if we can tell that the
11573 elements won't have reference type. */
11574 tree type = TREE_TYPE (pattern);
11575 if (type && TREE_CODE (type) != REFERENCE_TYPE
11576 && !PACK_EXPANSION_P (type)
11577 && !WILDCARD_TYPE_P (type))
11578 return args;
11579 /* Otherwise use the normal path so we get convert_from_reference. */
11580 }
11581
11582 /* We cannot expand this expansion expression, because we don't have
11583 all of the argument packs we need. */
11584 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11585 {
11586 /* We got some full packs, but we can't substitute them in until we
11587 have values for all the packs. So remember these until then. */
11588
11589 t = make_pack_expansion (pattern);
11590 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11591 return t;
11592 }
11593 else if (unsubstituted_packs)
11594 {
11595 /* There were no real arguments, we're just replacing a parameter
11596 pack with another version of itself. Substitute into the
11597 pattern and return a PACK_EXPANSION_*. The caller will need to
11598 deal with that. */
11599 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11600 t = tsubst_expr (pattern, args, complain, in_decl,
11601 /*integral_constant_expression_p=*/false);
11602 else
11603 t = tsubst (pattern, args, complain, in_decl);
11604 t = make_pack_expansion (t);
11605 return t;
11606 }
11607
11608 gcc_assert (len >= 0);
11609
11610 if (need_local_specializations)
11611 {
11612 /* We're in a late-specified return type, so create our own local
11613 specializations map; the current map is either NULL or (in the
11614 case of recursive unification) might have bindings that we don't
11615 want to use or alter. */
11616 saved_local_specializations = local_specializations;
11617 local_specializations = new hash_map<tree, tree>;
11618 }
11619
11620 /* For each argument in each argument pack, substitute into the
11621 pattern. */
11622 result = make_tree_vec (len);
11623 tree elem_args = copy_template_args (args);
11624 for (i = 0; i < len; ++i)
11625 {
11626 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11627 i,
11628 elem_args, complain,
11629 in_decl);
11630 TREE_VEC_ELT (result, i) = t;
11631 if (t == error_mark_node)
11632 {
11633 result = error_mark_node;
11634 break;
11635 }
11636 }
11637
11638 /* Update ARGS to restore the substitution from parameter packs to
11639 their argument packs. */
11640 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11641 {
11642 tree parm = TREE_PURPOSE (pack);
11643
11644 if (TREE_CODE (parm) == PARM_DECL
11645 || TREE_CODE (parm) == FIELD_DECL)
11646 register_local_specialization (TREE_TYPE (pack), parm);
11647 else
11648 {
11649 int idx, level;
11650
11651 if (TREE_VALUE (pack) == NULL_TREE)
11652 continue;
11653
11654 template_parm_level_and_index (parm, &level, &idx);
11655
11656 /* Update the corresponding argument. */
11657 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11658 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11659 TREE_TYPE (pack);
11660 else
11661 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11662 }
11663 }
11664
11665 if (need_local_specializations)
11666 {
11667 delete local_specializations;
11668 local_specializations = saved_local_specializations;
11669 }
11670
11671 /* If the dependent pack arguments were such that we end up with only a
11672 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11673 if (len == 1 && TREE_CODE (result) == TREE_VEC
11674 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11675 return TREE_VEC_ELT (result, 0);
11676
11677 return result;
11678}
11679
11680/* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11681 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11682 parameter packs; all parms generated from a function parameter pack will
11683 have the same DECL_PARM_INDEX. */
11684
11685tree
11686get_pattern_parm (tree parm, tree tmpl)
11687{
11688 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11689 tree patparm;
11690
11691 if (DECL_ARTIFICIAL (parm))
11692 {
11693 for (patparm = DECL_ARGUMENTS (pattern);
11694 patparm; patparm = DECL_CHAIN (patparm))
11695 if (DECL_ARTIFICIAL (patparm)
11696 && DECL_NAME (parm) == DECL_NAME (patparm))
11697 break;
11698 }
11699 else
11700 {
11701 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11702 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11703 gcc_assert (DECL_PARM_INDEX (patparm)
11704 == DECL_PARM_INDEX (parm));
11705 }
11706
11707 return patparm;
11708}
11709
11710/* Make an argument pack out of the TREE_VEC VEC. */
11711
11712static tree
11713make_argument_pack (tree vec)
11714{
11715 tree pack;
11716 tree elt = TREE_VEC_ELT (vec, 0);
11717 if (TYPE_P (elt))
11718 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11719 else
11720 {
11721 pack = make_node (NONTYPE_ARGUMENT_PACK);
11722 TREE_TYPE (pack) = TREE_TYPE (elt);
11723 TREE_CONSTANT (pack) = 1;
11724 }
11725 SET_ARGUMENT_PACK_ARGS (pack, vec);
11726 return pack;
11727}
11728
11729/* Return an exact copy of template args T that can be modified
11730 independently. */
11731
11732static tree
11733copy_template_args (tree t)
11734{
11735 if (t == error_mark_node)
11736 return t;
11737
11738 int len = TREE_VEC_LENGTH (t);
11739 tree new_vec = make_tree_vec (len);
11740
11741 for (int i = 0; i < len; ++i)
11742 {
11743 tree elt = TREE_VEC_ELT (t, i);
11744 if (elt && TREE_CODE (elt) == TREE_VEC)
11745 elt = copy_template_args (elt);
11746 TREE_VEC_ELT (new_vec, i) = elt;
11747 }
11748
11749 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11750 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11751
11752 return new_vec;
11753}
11754
11755/* Substitute ARGS into the vector or list of template arguments T. */
11756
11757static tree
11758tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11759{
11760 tree orig_t = t;
11761 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11762 tree *elts;
11763
11764 if (t == error_mark_node)
11765 return error_mark_node;
11766
11767 len = TREE_VEC_LENGTH (t);
11768 elts = XALLOCAVEC (tree, len);
11769
11770 for (i = 0; i < len; i++)
11771 {
11772 tree orig_arg = TREE_VEC_ELT (t, i);
11773 tree new_arg;
11774
11775 if (TREE_CODE (orig_arg) == TREE_VEC)
11776 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11777 else if (PACK_EXPANSION_P (orig_arg))
11778 {
11779 /* Substitute into an expansion expression. */
11780 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11781
11782 if (TREE_CODE (new_arg) == TREE_VEC)
11783 /* Add to the expanded length adjustment the number of
11784 expanded arguments. We subtract one from this
11785 measurement, because the argument pack expression
11786 itself is already counted as 1 in
11787 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11788 the argument pack is empty. */
11789 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11790 }
11791 else if (ARGUMENT_PACK_P (orig_arg))
11792 {
11793 /* Substitute into each of the arguments. */
11794 new_arg = TYPE_P (orig_arg)
11795 ? cxx_make_type (TREE_CODE (orig_arg))
11796 : make_node (TREE_CODE (orig_arg));
11797
11798 SET_ARGUMENT_PACK_ARGS (
11799 new_arg,
11800 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11801 args, complain, in_decl));
11802
11803 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11804 new_arg = error_mark_node;
11805
11806 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11807 if (type_uses_auto (TREE_TYPE (orig_arg)))
11808 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11809 else
11810 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11811 complain, in_decl);
11812 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11813
11814 if (TREE_TYPE (new_arg) == error_mark_node)
11815 new_arg = error_mark_node;
11816 }
11817 }
11818 else
11819 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11820
11821 if (new_arg == error_mark_node)
11822 return error_mark_node;
11823
11824 elts[i] = new_arg;
11825 if (new_arg != orig_arg)
11826 need_new = 1;
11827 }
11828
11829 if (!need_new)
11830 return t;
11831
11832 /* Make space for the expanded arguments coming from template
11833 argument packs. */
11834 t = make_tree_vec (len + expanded_len_adjust);
11835 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11836 arguments for a member template.
11837 In that case each TREE_VEC in ORIG_T represents a level of template
11838 arguments, and ORIG_T won't carry any non defaulted argument count.
11839 It will rather be the nested TREE_VECs that will carry one.
11840 In other words, ORIG_T carries a non defaulted argument count only
11841 if it doesn't contain any nested TREE_VEC. */
11842 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11843 {
11844 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11845 count += expanded_len_adjust;
11846 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11847 }
11848 for (i = 0, out = 0; i < len; i++)
11849 {
11850 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11851 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11852 && TREE_CODE (elts[i]) == TREE_VEC)
11853 {
11854 int idx;
11855
11856 /* Now expand the template argument pack "in place". */
11857 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11858 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11859 }
11860 else
11861 {
11862 TREE_VEC_ELT (t, out) = elts[i];
11863 out++;
11864 }
11865 }
11866
11867 return t;
11868}
11869
11870/* Substitute ARGS into one level PARMS of template parameters. */
11871
11872static tree
11873tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11874{
11875 if (parms == error_mark_node)
11876 return error_mark_node;
11877
11878 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11879
11880 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11881 {
11882 tree tuple = TREE_VEC_ELT (parms, i);
11883
11884 if (tuple == error_mark_node)
11885 continue;
11886
11887 TREE_VEC_ELT (new_vec, i) =
11888 tsubst_template_parm (tuple, args, complain);
11889 }
11890
11891 return new_vec;
11892}
11893
11894/* Return the result of substituting ARGS into the template parameters
11895 given by PARMS. If there are m levels of ARGS and m + n levels of
11896 PARMS, then the result will contain n levels of PARMS. For
11897 example, if PARMS is `template <class T> template <class U>
11898 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11899 result will be `template <int*, double, class V>'. */
11900
11901static tree
11902tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11903{
11904 tree r = NULL_TREE;
11905 tree* new_parms;
11906
11907 /* When substituting into a template, we must set
11908 PROCESSING_TEMPLATE_DECL as the template parameters may be
11909 dependent if they are based on one-another, and the dependency
11910 predicates are short-circuit outside of templates. */
11911 ++processing_template_decl;
11912
11913 for (new_parms = &r;
11914 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11915 new_parms = &(TREE_CHAIN (*new_parms)),
11916 parms = TREE_CHAIN (parms))
11917 {
11918 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11919 args, complain);
11920 *new_parms =
11921 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11922 - TMPL_ARGS_DEPTH (args)),
11923 new_vec, NULL_TREE);
11924 }
11925
11926 --processing_template_decl;
11927
11928 return r;
11929}
11930
11931/* Return the result of substituting ARGS into one template parameter
11932 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11933 parameter and which TREE_PURPOSE is the default argument of the
11934 template parameter. */
11935
11936static tree
11937tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11938{
11939 tree default_value, parm_decl;
11940
11941 if (args == NULL_TREE
11942 || t == NULL_TREE
11943 || t == error_mark_node)
11944 return t;
11945
11946 gcc_assert (TREE_CODE (t) == TREE_LIST);
11947
11948 default_value = TREE_PURPOSE (t);
11949 parm_decl = TREE_VALUE (t);
11950
11951 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11952 if (TREE_CODE (parm_decl) == PARM_DECL
11953 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11954 parm_decl = error_mark_node;
11955 default_value = tsubst_template_arg (default_value, args,
11956 complain, NULL_TREE);
11957
11958 return build_tree_list (default_value, parm_decl);
11959}
11960
11961/* Substitute the ARGS into the indicated aggregate (or enumeration)
11962 type T. If T is not an aggregate or enumeration type, it is
11963 handled as if by tsubst. IN_DECL is as for tsubst. If
11964 ENTERING_SCOPE is nonzero, T is the context for a template which
11965 we are presently tsubst'ing. Return the substituted value. */
11966
11967static tree
11968tsubst_aggr_type (tree t,
11969 tree args,
11970 tsubst_flags_t complain,
11971 tree in_decl,
11972 int entering_scope)
11973{
11974 if (t == NULL_TREE)
11975 return NULL_TREE;
11976
11977 switch (TREE_CODE (t))
11978 {
11979 case RECORD_TYPE:
11980 if (TYPE_PTRMEMFUNC_P (t))
11981 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11982
11983 /* Fall through. */
11984 case ENUMERAL_TYPE:
11985 case UNION_TYPE:
11986 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11987 {
11988 tree argvec;
11989 tree context;
11990 tree r;
11991 int saved_unevaluated_operand;
11992 int saved_inhibit_evaluation_warnings;
11993
11994 /* In "sizeof(X<I>)" we need to evaluate "I". */
11995 saved_unevaluated_operand = cp_unevaluated_operand;
11996 cp_unevaluated_operand = 0;
11997 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11998 c_inhibit_evaluation_warnings = 0;
11999
12000 /* First, determine the context for the type we are looking
12001 up. */
12002 context = TYPE_CONTEXT (t);
12003 if (context && TYPE_P (context))
12004 {
12005 context = tsubst_aggr_type (context, args, complain,
12006 in_decl, /*entering_scope=*/1);
12007 /* If context is a nested class inside a class template,
12008 it may still need to be instantiated (c++/33959). */
12009 context = complete_type (context);
12010 }
12011
12012 /* Then, figure out what arguments are appropriate for the
12013 type we are trying to find. For example, given:
12014
12015 template <class T> struct S;
12016 template <class T, class U> void f(T, U) { S<U> su; }
12017
12018 and supposing that we are instantiating f<int, double>,
12019 then our ARGS will be {int, double}, but, when looking up
12020 S we only want {double}. */
12021 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12022 complain, in_decl);
12023 if (argvec == error_mark_node)
12024 r = error_mark_node;
12025 else
12026 {
12027 r = lookup_template_class (t, argvec, in_decl, context,
12028 entering_scope, complain);
12029 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12030 }
12031
12032 cp_unevaluated_operand = saved_unevaluated_operand;
12033 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12034
12035 return r;
12036 }
12037 else
12038 /* This is not a template type, so there's nothing to do. */
12039 return t;
12040
12041 default:
12042 return tsubst (t, args, complain, in_decl);
12043 }
12044}
12045
12046/* Substitute into the default argument ARG (a default argument for
12047 FN), which has the indicated TYPE. */
12048
12049tree
12050tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
12051{
12052 tree saved_class_ptr = NULL_TREE;
12053 tree saved_class_ref = NULL_TREE;
12054 int errs = errorcount + sorrycount;
12055
12056 /* This can happen in invalid code. */
12057 if (TREE_CODE (arg) == DEFAULT_ARG)
12058 return arg;
12059
12060 /* This default argument came from a template. Instantiate the
12061 default argument here, not in tsubst. In the case of
12062 something like:
12063
12064 template <class T>
12065 struct S {
12066 static T t();
12067 void f(T = t());
12068 };
12069
12070 we must be careful to do name lookup in the scope of S<T>,
12071 rather than in the current class. */
12072 push_access_scope (fn);
12073 /* The "this" pointer is not valid in a default argument. */
12074 if (cfun)
12075 {
12076 saved_class_ptr = current_class_ptr;
12077 cp_function_chain->x_current_class_ptr = NULL_TREE;
12078 saved_class_ref = current_class_ref;
12079 cp_function_chain->x_current_class_ref = NULL_TREE;
12080 }
12081
12082 push_deferring_access_checks(dk_no_deferred);
12083 /* The default argument expression may cause implicitly defined
12084 member functions to be synthesized, which will result in garbage
12085 collection. We must treat this situation as if we were within
12086 the body of function so as to avoid collecting live data on the
12087 stack. */
12088 ++function_depth;
12089 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12090 complain, NULL_TREE,
12091 /*integral_constant_expression_p=*/false);
12092 --function_depth;
12093 pop_deferring_access_checks();
12094
12095 /* Restore the "this" pointer. */
12096 if (cfun)
12097 {
12098 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12099 cp_function_chain->x_current_class_ref = saved_class_ref;
12100 }
12101
12102 if (errorcount+sorrycount > errs
12103 && (complain & tf_warning_or_error))
12104 inform (input_location,
12105 " when instantiating default argument for call to %D", fn);
12106
12107 /* Make sure the default argument is reasonable. */
12108 arg = check_default_argument (type, arg, complain);
12109
12110 pop_access_scope (fn);
12111
12112 return arg;
12113}
12114
12115/* Substitute into all the default arguments for FN. */
12116
12117static void
12118tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12119{
12120 tree arg;
12121 tree tmpl_args;
12122
12123 tmpl_args = DECL_TI_ARGS (fn);
12124
12125 /* If this function is not yet instantiated, we certainly don't need
12126 its default arguments. */
12127 if (uses_template_parms (tmpl_args))
12128 return;
12129 /* Don't do this again for clones. */
12130 if (DECL_CLONED_FUNCTION_P (fn))
12131 return;
12132
12133 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12134 arg;
12135 arg = TREE_CHAIN (arg))
12136 if (TREE_PURPOSE (arg))
12137 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12138 TREE_VALUE (arg),
12139 TREE_PURPOSE (arg),
12140 complain);
12141}
12142
12143/* Substitute the ARGS into the T, which is a _DECL. Return the
12144 result of the substitution. Issue error and warning messages under
12145 control of COMPLAIN. */
12146
12147static tree
12148tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12149{
12150#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12151 location_t saved_loc;
12152 tree r = NULL_TREE;
12153 tree in_decl = t;
12154 hashval_t hash = 0;
12155
12156 /* Set the filename and linenumber to improve error-reporting. */
12157 saved_loc = input_location;
12158 input_location = DECL_SOURCE_LOCATION (t);
12159
12160 switch (TREE_CODE (t))
12161 {
12162 case TEMPLATE_DECL:
12163 {
12164 /* We can get here when processing a member function template,
12165 member class template, or template template parameter. */
12166 tree decl = DECL_TEMPLATE_RESULT (t);
12167 tree spec;
12168 tree tmpl_args;
12169 tree full_args;
12170
12171 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12172 {
12173 /* Template template parameter is treated here. */
12174 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12175 if (new_type == error_mark_node)
12176 r = error_mark_node;
12177 /* If we get a real template back, return it. This can happen in
12178 the context of most_specialized_partial_spec. */
12179 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12180 r = new_type;
12181 else
12182 /* The new TEMPLATE_DECL was built in
12183 reduce_template_parm_level. */
12184 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12185 break;
12186 }
12187
12188 /* We might already have an instance of this template.
12189 The ARGS are for the surrounding class type, so the
12190 full args contain the tsubst'd args for the context,
12191 plus the innermost args from the template decl. */
12192 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12193 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12194 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12195 /* Because this is a template, the arguments will still be
12196 dependent, even after substitution. If
12197 PROCESSING_TEMPLATE_DECL is not set, the dependency
12198 predicates will short-circuit. */
12199 ++processing_template_decl;
12200 full_args = tsubst_template_args (tmpl_args, args,
12201 complain, in_decl);
12202 --processing_template_decl;
12203 if (full_args == error_mark_node)
12204 RETURN (error_mark_node);
12205
12206 /* If this is a default template template argument,
12207 tsubst might not have changed anything. */
12208 if (full_args == tmpl_args)
12209 RETURN (t);
12210
12211 hash = hash_tmpl_and_args (t, full_args);
12212 spec = retrieve_specialization (t, full_args, hash);
12213 if (spec != NULL_TREE)
12214 {
12215 r = spec;
12216 break;
12217 }
12218
12219 /* Make a new template decl. It will be similar to the
12220 original, but will record the current template arguments.
12221 We also create a new function declaration, which is just
12222 like the old one, but points to this new template, rather
12223 than the old one. */
12224 r = copy_decl (t);
12225 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12226 DECL_CHAIN (r) = NULL_TREE;
12227
12228 // Build new template info linking to the original template decl.
12229 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12230
12231 if (TREE_CODE (decl) == TYPE_DECL
12232 && !TYPE_DECL_ALIAS_P (decl))
12233 {
12234 tree new_type;
12235 ++processing_template_decl;
12236 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12237 --processing_template_decl;
12238 if (new_type == error_mark_node)
12239 RETURN (error_mark_node);
12240
12241 TREE_TYPE (r) = new_type;
12242 /* For a partial specialization, we need to keep pointing to
12243 the primary template. */
12244 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12245 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12246 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12247 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12248 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12249 }
12250 else
12251 {
12252 tree new_decl;
12253 ++processing_template_decl;
12254 new_decl = tsubst (decl, args, complain, in_decl);
12255 --processing_template_decl;
12256 if (new_decl == error_mark_node)
12257 RETURN (error_mark_node);
12258
12259 DECL_TEMPLATE_RESULT (r) = new_decl;
12260 DECL_TI_TEMPLATE (new_decl) = r;
12261 TREE_TYPE (r) = TREE_TYPE (new_decl);
12262 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12263 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12264 }
12265
12266 SET_DECL_IMPLICIT_INSTANTIATION (r);
12267 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12268 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12269
12270 /* The template parameters for this new template are all the
12271 template parameters for the old template, except the
12272 outermost level of parameters. */
12273 DECL_TEMPLATE_PARMS (r)
12274 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12275 complain);
12276
12277 if (PRIMARY_TEMPLATE_P (t))
12278 DECL_PRIMARY_TEMPLATE (r) = r;
12279
12280 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12281 /* Record this non-type partial instantiation. */
12282 register_specialization (r, t,
12283 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12284 false, hash);
12285 }
12286 break;
12287
12288 case FUNCTION_DECL:
12289 {
12290 tree ctx;
12291 tree argvec = NULL_TREE;
12292 tree *friends;
12293 tree gen_tmpl;
12294 tree type;
12295 int member;
12296 int args_depth;
12297 int parms_depth;
12298
12299 /* Nobody should be tsubst'ing into non-template functions. */
12300 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12301
12302 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12303 {
12304 tree spec;
12305
12306 /* If T is not dependent, just return it. */
12307 if (!uses_template_parms (DECL_TI_ARGS (t)))
12308 RETURN (t);
12309
12310 /* Calculate the most general template of which R is a
12311 specialization, and the complete set of arguments used to
12312 specialize R. */
12313 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12314 argvec = tsubst_template_args (DECL_TI_ARGS
12315 (DECL_TEMPLATE_RESULT
12316 (DECL_TI_TEMPLATE (t))),
12317 args, complain, in_decl);
12318 if (argvec == error_mark_node)
12319 RETURN (error_mark_node);
12320
12321 /* Check to see if we already have this specialization. */
12322 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12323 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12324
12325 if (spec)
12326 {
12327 r = spec;
12328 break;
12329 }
12330
12331 /* We can see more levels of arguments than parameters if
12332 there was a specialization of a member template, like
12333 this:
12334
12335 template <class T> struct S { template <class U> void f(); }
12336 template <> template <class U> void S<int>::f(U);
12337
12338 Here, we'll be substituting into the specialization,
12339 because that's where we can find the code we actually
12340 want to generate, but we'll have enough arguments for
12341 the most general template.
12342
12343 We also deal with the peculiar case:
12344
12345 template <class T> struct S {
12346 template <class U> friend void f();
12347 };
12348 template <class U> void f() {}
12349 template S<int>;
12350 template void f<double>();
12351
12352 Here, the ARGS for the instantiation of will be {int,
12353 double}. But, we only need as many ARGS as there are
12354 levels of template parameters in CODE_PATTERN. We are
12355 careful not to get fooled into reducing the ARGS in
12356 situations like:
12357
12358 template <class T> struct S { template <class U> void f(U); }
12359 template <class T> template <> void S<T>::f(int) {}
12360
12361 which we can spot because the pattern will be a
12362 specialization in this case. */
12363 args_depth = TMPL_ARGS_DEPTH (args);
12364 parms_depth =
12365 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12366 if (args_depth > parms_depth
12367 && !DECL_TEMPLATE_SPECIALIZATION (t))
12368 args = get_innermost_template_args (args, parms_depth);
12369 }
12370 else
12371 {
12372 /* This special case arises when we have something like this:
12373
12374 template <class T> struct S {
12375 friend void f<int>(int, double);
12376 };
12377
12378 Here, the DECL_TI_TEMPLATE for the friend declaration
12379 will be an IDENTIFIER_NODE. We are being called from
12380 tsubst_friend_function, and we want only to create a
12381 new decl (R) with appropriate types so that we can call
12382 determine_specialization. */
12383 gen_tmpl = NULL_TREE;
12384 }
12385
12386 if (DECL_CLASS_SCOPE_P (t))
12387 {
12388 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12389 member = 2;
12390 else
12391 member = 1;
12392 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12393 complain, t, /*entering_scope=*/1);
12394 }
12395 else
12396 {
12397 member = 0;
12398 ctx = DECL_CONTEXT (t);
12399 }
12400 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12401 if (type == error_mark_node)
12402 RETURN (error_mark_node);
12403
12404 /* If we hit excessive deduction depth, the type is bogus even if
12405 it isn't error_mark_node, so don't build a decl. */
12406 if (excessive_deduction_depth)
12407 RETURN (error_mark_node);
12408
12409 /* We do NOT check for matching decls pushed separately at this
12410 point, as they may not represent instantiations of this
12411 template, and in any case are considered separate under the
12412 discrete model. */
12413 r = copy_decl (t);
12414 DECL_USE_TEMPLATE (r) = 0;
12415 TREE_TYPE (r) = type;
12416 /* Clear out the mangled name and RTL for the instantiation. */
12417 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12418 SET_DECL_RTL (r, NULL);
12419 /* Leave DECL_INITIAL set on deleted instantiations. */
12420 if (!DECL_DELETED_FN (r))
12421 DECL_INITIAL (r) = NULL_TREE;
12422 DECL_CONTEXT (r) = ctx;
12423
12424 /* OpenMP UDRs have the only argument a reference to the declared
12425 type. We want to diagnose if the declared type is a reference,
12426 which is invalid, but as references to references are usually
12427 quietly merged, diagnose it here. */
12428 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12429 {
12430 tree argtype
12431 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12432 argtype = tsubst (argtype, args, complain, in_decl);
12433 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12434 error_at (DECL_SOURCE_LOCATION (t),
12435 "reference type %qT in "
12436 "%<#pragma omp declare reduction%>", argtype);
12437 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12438 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12439 argtype);
12440 }
12441
12442 if (member && DECL_CONV_FN_P (r))
12443 /* Type-conversion operator. Reconstruct the name, in
12444 case it's the name of one of the template's parameters. */
12445 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12446
12447 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12448 complain, t);
12449 DECL_RESULT (r) = NULL_TREE;
12450
12451 TREE_STATIC (r) = 0;
12452 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12453 DECL_EXTERNAL (r) = 1;
12454 /* If this is an instantiation of a function with internal
12455 linkage, we already know what object file linkage will be
12456 assigned to the instantiation. */
12457 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12458 DECL_DEFER_OUTPUT (r) = 0;
12459 DECL_CHAIN (r) = NULL_TREE;
12460 DECL_PENDING_INLINE_INFO (r) = 0;
12461 DECL_PENDING_INLINE_P (r) = 0;
12462 DECL_SAVED_TREE (r) = NULL_TREE;
12463 DECL_STRUCT_FUNCTION (r) = NULL;
12464 TREE_USED (r) = 0;
12465 /* We'll re-clone as appropriate in instantiate_template. */
12466 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12467
12468 /* If we aren't complaining now, return on error before we register
12469 the specialization so that we'll complain eventually. */
12470 if ((complain & tf_error) == 0
12471 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12472 && !grok_op_properties (r, /*complain=*/false))
12473 RETURN (error_mark_node);
12474
12475 /* When instantiating a constrained member, substitute
12476 into the constraints to create a new constraint. */
12477 if (tree ci = get_constraints (t))
12478 if (member)
12479 {
12480 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12481 set_constraints (r, ci);
12482 }
12483
12484 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12485 this in the special friend case mentioned above where
12486 GEN_TMPL is NULL. */
12487 if (gen_tmpl)
12488 {
12489 DECL_TEMPLATE_INFO (r)
12490 = build_template_info (gen_tmpl, argvec);
12491 SET_DECL_IMPLICIT_INSTANTIATION (r);
12492
12493 tree new_r
12494 = register_specialization (r, gen_tmpl, argvec, false, hash);
12495 if (new_r != r)
12496 /* We instantiated this while substituting into
12497 the type earlier (template/friend54.C). */
12498 RETURN (new_r);
12499
12500 /* We're not supposed to instantiate default arguments
12501 until they are called, for a template. But, for a
12502 declaration like:
12503
12504 template <class T> void f ()
12505 { extern void g(int i = T()); }
12506
12507 we should do the substitution when the template is
12508 instantiated. We handle the member function case in
12509 instantiate_class_template since the default arguments
12510 might refer to other members of the class. */
12511 if (!member
12512 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12513 && !uses_template_parms (argvec))
12514 tsubst_default_arguments (r, complain);
12515 }
12516 else
12517 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12518
12519 /* Copy the list of befriending classes. */
12520 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12521 *friends;
12522 friends = &TREE_CHAIN (*friends))
12523 {
12524 *friends = copy_node (*friends);
12525 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12526 args, complain,
12527 in_decl);
12528 }
12529
12530 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12531 {
12532 maybe_retrofit_in_chrg (r);
12533 if (DECL_CONSTRUCTOR_P (r))
12534 grok_ctor_properties (ctx, r);
12535 /* If this is an instantiation of a member template, clone it.
12536 If it isn't, that'll be handled by
12537 clone_constructors_and_destructors. */
12538 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12539 clone_function_decl (r, /*update_method_vec_p=*/0);
12540 }
12541 else if ((complain & tf_error) != 0
12542 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12543 && !grok_op_properties (r, /*complain=*/true))
12544 RETURN (error_mark_node);
12545
12546 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12547 SET_DECL_FRIEND_CONTEXT (r,
12548 tsubst (DECL_FRIEND_CONTEXT (t),
12549 args, complain, in_decl));
12550
12551 /* Possibly limit visibility based on template args. */
12552 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12553 if (DECL_VISIBILITY_SPECIFIED (t))
12554 {
12555 DECL_VISIBILITY_SPECIFIED (r) = 0;
12556 DECL_ATTRIBUTES (r)
12557 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12558 }
12559 determine_visibility (r);
12560 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12561 && !processing_template_decl)
12562 defaulted_late_check (r);
12563
12564 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12565 args, complain, in_decl);
12566 }
12567 break;
12568
12569 case PARM_DECL:
12570 {
12571 tree type = NULL_TREE;
12572 int i, len = 1;
12573 tree expanded_types = NULL_TREE;
12574 tree prev_r = NULL_TREE;
12575 tree first_r = NULL_TREE;
12576
12577 if (DECL_PACK_P (t))
12578 {
12579 /* If there is a local specialization that isn't a
12580 parameter pack, it means that we're doing a "simple"
12581 substitution from inside tsubst_pack_expansion. Just
12582 return the local specialization (which will be a single
12583 parm). */
12584 tree spec = retrieve_local_specialization (t);
12585 if (spec
12586 && TREE_CODE (spec) == PARM_DECL
12587 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12588 RETURN (spec);
12589
12590 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12591 the parameters in this function parameter pack. */
12592 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12593 complain, in_decl);
12594 if (TREE_CODE (expanded_types) == TREE_VEC)
12595 {
12596 len = TREE_VEC_LENGTH (expanded_types);
12597
12598 /* Zero-length parameter packs are boring. Just substitute
12599 into the chain. */
12600 if (len == 0)
12601 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12602 TREE_CHAIN (t)));
12603 }
12604 else
12605 {
12606 /* All we did was update the type. Make a note of that. */
12607 type = expanded_types;
12608 expanded_types = NULL_TREE;
12609 }
12610 }
12611
12612 /* Loop through all of the parameters we'll build. When T is
12613 a function parameter pack, LEN is the number of expanded
12614 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12615 r = NULL_TREE;
12616 for (i = 0; i < len; ++i)
12617 {
12618 prev_r = r;
12619 r = copy_node (t);
12620 if (DECL_TEMPLATE_PARM_P (t))
12621 SET_DECL_TEMPLATE_PARM_P (r);
12622
12623 if (expanded_types)
12624 /* We're on the Ith parameter of the function parameter
12625 pack. */
12626 {
12627 /* Get the Ith type. */
12628 type = TREE_VEC_ELT (expanded_types, i);
12629
12630 /* Rename the parameter to include the index. */
12631 DECL_NAME (r)
12632 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12633 }
12634 else if (!type)
12635 /* We're dealing with a normal parameter. */
12636 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12637
12638 type = type_decays_to (type);
12639 TREE_TYPE (r) = type;
12640 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12641
12642 if (DECL_INITIAL (r))
12643 {
12644 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12645 DECL_INITIAL (r) = TREE_TYPE (r);
12646 else
12647 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12648 complain, in_decl);
12649 }
12650
12651 DECL_CONTEXT (r) = NULL_TREE;
12652
12653 if (!DECL_TEMPLATE_PARM_P (r))
12654 DECL_ARG_TYPE (r) = type_passed_as (type);
12655
12656 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12657 args, complain, in_decl);
12658
12659 /* Keep track of the first new parameter we
12660 generate. That's what will be returned to the
12661 caller. */
12662 if (!first_r)
12663 first_r = r;
12664
12665 /* Build a proper chain of parameters when substituting
12666 into a function parameter pack. */
12667 if (prev_r)
12668 DECL_CHAIN (prev_r) = r;
12669 }
12670
12671 /* If cp_unevaluated_operand is set, we're just looking for a
12672 single dummy parameter, so don't keep going. */
12673 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12674 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12675 complain, DECL_CHAIN (t));
12676
12677 /* FIRST_R contains the start of the chain we've built. */
12678 r = first_r;
12679 }
12680 break;
12681
12682 case FIELD_DECL:
12683 {
12684 tree type = NULL_TREE;
12685 tree vec = NULL_TREE;
12686 tree expanded_types = NULL_TREE;
12687 int len = 1;
12688
12689 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12690 {
12691 /* This field is a lambda capture pack. Return a TREE_VEC of
12692 the expanded fields to instantiate_class_template_1 and
12693 store them in the specializations hash table as a
12694 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12695 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12696 complain, in_decl);
12697 if (TREE_CODE (expanded_types) == TREE_VEC)
12698 {
12699 len = TREE_VEC_LENGTH (expanded_types);
12700 vec = make_tree_vec (len);
12701 }
12702 else
12703 {
12704 /* All we did was update the type. Make a note of that. */
12705 type = expanded_types;
12706 expanded_types = NULL_TREE;
12707 }
12708 }
12709
12710 for (int i = 0; i < len; ++i)
12711 {
12712 r = copy_decl (t);
12713 if (expanded_types)
12714 {
12715 type = TREE_VEC_ELT (expanded_types, i);
12716 DECL_NAME (r)
12717 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12718 }
12719 else if (!type)
12720 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12721
12722 if (type == error_mark_node)
12723 RETURN (error_mark_node);
12724 TREE_TYPE (r) = type;
12725 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12726
12727 if (DECL_C_BIT_FIELD (r))
12728 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12729 non-bit-fields DECL_INITIAL is a non-static data member
12730 initializer, which gets deferred instantiation. */
12731 DECL_INITIAL (r)
12732 = tsubst_expr (DECL_INITIAL (t), args,
12733 complain, in_decl,
12734 /*integral_constant_expression_p=*/true);
12735 else if (DECL_INITIAL (t))
12736 {
12737 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12738 NSDMI in perform_member_init. Still set DECL_INITIAL
12739 so that we know there is one. */
12740 DECL_INITIAL (r) = void_node;
12741 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12742 retrofit_lang_decl (r);
12743 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12744 }
12745 /* We don't have to set DECL_CONTEXT here; it is set by
12746 finish_member_declaration. */
12747 DECL_CHAIN (r) = NULL_TREE;
12748
12749 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12750 args, complain, in_decl);
12751
12752 if (vec)
12753 TREE_VEC_ELT (vec, i) = r;
12754 }
12755
12756 if (vec)
12757 {
12758 r = vec;
12759 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12760 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12761 SET_ARGUMENT_PACK_ARGS (pack, vec);
12762 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12763 TREE_TYPE (pack) = tpack;
12764 register_specialization (pack, t, args, false, 0);
12765 }
12766 }
12767 break;
12768
12769 case USING_DECL:
12770 /* We reach here only for member using decls. We also need to check
12771 uses_template_parms because DECL_DEPENDENT_P is not set for a
12772 using-declaration that designates a member of the current
12773 instantiation (c++/53549). */
12774 if (DECL_DEPENDENT_P (t)
12775 || uses_template_parms (USING_DECL_SCOPE (t)))
12776 {
12777 tree scope = USING_DECL_SCOPE (t);
12778 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12779 if (PACK_EXPANSION_P (scope))
12780 {
12781 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12782 int len = TREE_VEC_LENGTH (vec);
12783 r = make_tree_vec (len);
12784 for (int i = 0; i < len; ++i)
12785 {
12786 tree escope = TREE_VEC_ELT (vec, i);
12787 tree elt = do_class_using_decl (escope, name);
12788 if (!elt)
12789 {
12790 r = error_mark_node;
12791 break;
12792 }
12793 else
12794 {
12795 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12796 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12797 }
12798 TREE_VEC_ELT (r, i) = elt;
12799 }
12800 }
12801 else
12802 {
12803 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12804 complain, in_decl);
12805 r = do_class_using_decl (inst_scope, name);
12806 if (!r)
12807 r = error_mark_node;
12808 else
12809 {
12810 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12811 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12812 }
12813 }
12814 }
12815 else
12816 {
12817 r = copy_node (t);
12818 DECL_CHAIN (r) = NULL_TREE;
12819 }
12820 break;
12821
12822 case TYPE_DECL:
12823 case VAR_DECL:
12824 {
12825 tree argvec = NULL_TREE;
12826 tree gen_tmpl = NULL_TREE;
12827 tree spec;
12828 tree tmpl = NULL_TREE;
12829 tree ctx;
12830 tree type = NULL_TREE;
12831 bool local_p;
12832
12833 if (TREE_TYPE (t) == error_mark_node)
12834 RETURN (error_mark_node);
12835
12836 if (TREE_CODE (t) == TYPE_DECL
12837 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12838 {
12839 /* If this is the canonical decl, we don't have to
12840 mess with instantiations, and often we can't (for
12841 typename, template type parms and such). Note that
12842 TYPE_NAME is not correct for the above test if
12843 we've copied the type for a typedef. */
12844 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12845 if (type == error_mark_node)
12846 RETURN (error_mark_node);
12847 r = TYPE_NAME (type);
12848 break;
12849 }
12850
12851 /* Check to see if we already have the specialization we
12852 need. */
12853 spec = NULL_TREE;
12854 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12855 {
12856 /* T is a static data member or namespace-scope entity.
12857 We have to substitute into namespace-scope variables
12858 (not just variable templates) because of cases like:
12859
12860 template <class T> void f() { extern T t; }
12861
12862 where the entity referenced is not known until
12863 instantiation time. */
12864 local_p = false;
12865 ctx = DECL_CONTEXT (t);
12866 if (DECL_CLASS_SCOPE_P (t))
12867 {
12868 ctx = tsubst_aggr_type (ctx, args,
12869 complain,
12870 in_decl, /*entering_scope=*/1);
12871 /* If CTX is unchanged, then T is in fact the
12872 specialization we want. That situation occurs when
12873 referencing a static data member within in its own
12874 class. We can use pointer equality, rather than
12875 same_type_p, because DECL_CONTEXT is always
12876 canonical... */
12877 if (ctx == DECL_CONTEXT (t)
12878 /* ... unless T is a member template; in which
12879 case our caller can be willing to create a
12880 specialization of that template represented
12881 by T. */
12882 && !(DECL_TI_TEMPLATE (t)
12883 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12884 spec = t;
12885 }
12886
12887 if (!spec)
12888 {
12889 tmpl = DECL_TI_TEMPLATE (t);
12890 gen_tmpl = most_general_template (tmpl);
12891 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12892 if (argvec != error_mark_node)
12893 argvec = (coerce_innermost_template_parms
12894 (DECL_TEMPLATE_PARMS (gen_tmpl),
12895 argvec, t, complain,
12896 /*all*/true, /*defarg*/true));
12897 if (argvec == error_mark_node)
12898 RETURN (error_mark_node);
12899 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12900 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12901 }
12902 }
12903 else
12904 {
12905 /* A local variable. */
12906 local_p = true;
12907 /* Subsequent calls to pushdecl will fill this in. */
12908 ctx = NULL_TREE;
12909 /* Unless this is a reference to a static variable from an
12910 enclosing function, in which case we need to fill it in now. */
12911 if (TREE_STATIC (t))
12912 {
12913 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12914 if (fn != current_function_decl)
12915 ctx = fn;
12916 }
12917 spec = retrieve_local_specialization (t);
12918 }
12919 /* If we already have the specialization we need, there is
12920 nothing more to do. */
12921 if (spec)
12922 {
12923 r = spec;
12924 break;
12925 }
12926
12927 /* Create a new node for the specialization we need. */
12928 r = copy_decl (t);
12929 if (type == NULL_TREE)
12930 {
12931 if (is_typedef_decl (t))
12932 type = DECL_ORIGINAL_TYPE (t);
12933 else
12934 type = TREE_TYPE (t);
12935 if (VAR_P (t)
12936 && VAR_HAD_UNKNOWN_BOUND (t)
12937 && type != error_mark_node)
12938 type = strip_array_domain (type);
12939 tree sub_args = args;
12940 if (tree auto_node = type_uses_auto (type))
12941 {
12942 /* Mask off any template args past the variable's context so we
12943 don't replace the auto with an unrelated argument. */
12944 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
12945 int extra = TMPL_ARGS_DEPTH (args) - nouter;
12946 if (extra > 0)
12947 sub_args = strip_innermost_template_args (args, extra);
12948 }
12949 type = tsubst (type, sub_args, complain, in_decl);
12950 }
12951 if (VAR_P (r))
12952 {
12953 /* Even if the original location is out of scope, the
12954 newly substituted one is not. */
12955 DECL_DEAD_FOR_LOCAL (r) = 0;
12956 DECL_INITIALIZED_P (r) = 0;
12957 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12958 if (type == error_mark_node)
12959 RETURN (error_mark_node);
12960 if (TREE_CODE (type) == FUNCTION_TYPE)
12961 {
12962 /* It may seem that this case cannot occur, since:
12963
12964 typedef void f();
12965 void g() { f x; }
12966
12967 declares a function, not a variable. However:
12968
12969 typedef void f();
12970 template <typename T> void g() { T t; }
12971 template void g<f>();
12972
12973 is an attempt to declare a variable with function
12974 type. */
12975 error ("variable %qD has function type",
12976 /* R is not yet sufficiently initialized, so we
12977 just use its name. */
12978 DECL_NAME (r));
12979 RETURN (error_mark_node);
12980 }
12981 type = complete_type (type);
12982 /* Wait until cp_finish_decl to set this again, to handle
12983 circular dependency (template/instantiate6.C). */
12984 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12985 type = check_var_type (DECL_NAME (r), type);
12986
12987 if (DECL_HAS_VALUE_EXPR_P (t))
12988 {
12989 tree ve = DECL_VALUE_EXPR (t);
12990 ve = tsubst_expr (ve, args, complain, in_decl,
12991 /*constant_expression_p=*/false);
12992 if (REFERENCE_REF_P (ve))
12993 {
12994 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12995 ve = TREE_OPERAND (ve, 0);
12996 }
12997 SET_DECL_VALUE_EXPR (r, ve);
12998 }
12999 if (CP_DECL_THREAD_LOCAL_P (r)
13000 && !processing_template_decl)
13001 set_decl_tls_model (r, decl_default_tls_model (r));
13002 }
13003 else if (DECL_SELF_REFERENCE_P (t))
13004 SET_DECL_SELF_REFERENCE_P (r);
13005 TREE_TYPE (r) = type;
13006 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13007 DECL_CONTEXT (r) = ctx;
13008 /* Clear out the mangled name and RTL for the instantiation. */
13009 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13010 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13011 SET_DECL_RTL (r, NULL);
13012 /* The initializer must not be expanded until it is required;
13013 see [temp.inst]. */
13014 DECL_INITIAL (r) = NULL_TREE;
13015 if (VAR_P (r))
13016 SET_DECL_MODE (r, VOIDmode);
13017 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13018 SET_DECL_RTL (r, NULL);
13019 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13020 if (VAR_P (r))
13021 {
13022 /* Possibly limit visibility based on template args. */
13023 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13024 if (DECL_VISIBILITY_SPECIFIED (t))
13025 {
13026 DECL_VISIBILITY_SPECIFIED (r) = 0;
13027 DECL_ATTRIBUTES (r)
13028 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13029 }
13030 determine_visibility (r);
13031 }
13032
13033 if (!local_p)
13034 {
13035 /* A static data member declaration is always marked
13036 external when it is declared in-class, even if an
13037 initializer is present. We mimic the non-template
13038 processing here. */
13039 DECL_EXTERNAL (r) = 1;
13040 if (DECL_NAMESPACE_SCOPE_P (t))
13041 DECL_NOT_REALLY_EXTERN (r) = 1;
13042
13043 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13044 SET_DECL_IMPLICIT_INSTANTIATION (r);
13045 register_specialization (r, gen_tmpl, argvec, false, hash);
13046 }
13047 else
13048 {
13049 if (DECL_LANG_SPECIFIC (r))
13050 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13051 if (!cp_unevaluated_operand)
13052 register_local_specialization (r, t);
13053 }
13054
13055 DECL_CHAIN (r) = NULL_TREE;
13056
13057 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13058 /*flags=*/0,
13059 args, complain, in_decl);
13060
13061 /* Preserve a typedef that names a type. */
13062 if (is_typedef_decl (r) && type != error_mark_node)
13063 {
13064 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13065 set_underlying_type (r);
13066 if (TYPE_DECL_ALIAS_P (r))
13067 /* An alias template specialization can be dependent
13068 even if its underlying type is not. */
13069 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13070 }
13071
13072 layout_decl (r, 0);
13073 }
13074 break;
13075
13076 default:
13077 gcc_unreachable ();
13078 }
13079#undef RETURN
13080
13081 out:
13082 /* Restore the file and line information. */
13083 input_location = saved_loc;
13084
13085 return r;
13086}
13087
13088/* Substitute into the ARG_TYPES of a function type.
13089 If END is a TREE_CHAIN, leave it and any following types
13090 un-substituted. */
13091
13092static tree
13093tsubst_arg_types (tree arg_types,
13094 tree args,
13095 tree end,
13096 tsubst_flags_t complain,
13097 tree in_decl)
13098{
13099 tree remaining_arg_types;
13100 tree type = NULL_TREE;
13101 int i = 1;
13102 tree expanded_args = NULL_TREE;
13103 tree default_arg;
13104
13105 if (!arg_types || arg_types == void_list_node || arg_types == end)
13106 return arg_types;
13107
13108 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13109 args, end, complain, in_decl);
13110 if (remaining_arg_types == error_mark_node)
13111 return error_mark_node;
13112
13113 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13114 {
13115 /* For a pack expansion, perform substitution on the
13116 entire expression. Later on, we'll handle the arguments
13117 one-by-one. */
13118 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13119 args, complain, in_decl);
13120
13121 if (TREE_CODE (expanded_args) == TREE_VEC)
13122 /* So that we'll spin through the parameters, one by one. */
13123 i = TREE_VEC_LENGTH (expanded_args);
13124 else
13125 {
13126 /* We only partially substituted into the parameter
13127 pack. Our type is TYPE_PACK_EXPANSION. */
13128 type = expanded_args;
13129 expanded_args = NULL_TREE;
13130 }
13131 }
13132
13133 while (i > 0) {
13134 --i;
13135
13136 if (expanded_args)
13137 type = TREE_VEC_ELT (expanded_args, i);
13138 else if (!type)
13139 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13140
13141 if (type == error_mark_node)
13142 return error_mark_node;
13143 if (VOID_TYPE_P (type))
13144 {
13145 if (complain & tf_error)
13146 {
13147 error ("invalid parameter type %qT", type);
13148 if (in_decl)
13149 error ("in declaration %q+D", in_decl);
13150 }
13151 return error_mark_node;
13152 }
13153 /* DR 657. */
13154 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13155 return error_mark_node;
13156
13157 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13158 top-level qualifiers as required. */
13159 type = cv_unqualified (type_decays_to (type));
13160
13161 /* We do not substitute into default arguments here. The standard
13162 mandates that they be instantiated only when needed, which is
13163 done in build_over_call. */
13164 default_arg = TREE_PURPOSE (arg_types);
13165
13166 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13167 {
13168 /* We've instantiated a template before its default arguments
13169 have been parsed. This can happen for a nested template
13170 class, and is not an error unless we require the default
13171 argument in a call of this function. */
13172 remaining_arg_types =
13173 tree_cons (default_arg, type, remaining_arg_types);
13174 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13175 }
13176 else
13177 remaining_arg_types =
13178 hash_tree_cons (default_arg, type, remaining_arg_types);
13179 }
13180
13181 return remaining_arg_types;
13182}
13183
13184/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13185 *not* handle the exception-specification for FNTYPE, because the
13186 initial substitution of explicitly provided template parameters
13187 during argument deduction forbids substitution into the
13188 exception-specification:
13189
13190 [temp.deduct]
13191
13192 All references in the function type of the function template to the
13193 corresponding template parameters are replaced by the specified tem-
13194 plate argument values. If a substitution in a template parameter or
13195 in the function type of the function template results in an invalid
13196 type, type deduction fails. [Note: The equivalent substitution in
13197 exception specifications is done only when the function is instanti-
13198 ated, at which point a program is ill-formed if the substitution
13199 results in an invalid type.] */
13200
13201static tree
13202tsubst_function_type (tree t,
13203 tree args,
13204 tsubst_flags_t complain,
13205 tree in_decl)
13206{
13207 tree return_type;
13208 tree arg_types = NULL_TREE;
13209 tree fntype;
13210
13211 /* The TYPE_CONTEXT is not used for function/method types. */
13212 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13213
13214 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13215 failure. */
13216 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13217
13218 if (late_return_type_p)
13219 {
13220 /* Substitute the argument types. */
13221 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13222 complain, in_decl);
13223 if (arg_types == error_mark_node)
13224 return error_mark_node;
13225
13226 tree save_ccp = current_class_ptr;
13227 tree save_ccr = current_class_ref;
13228 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13229 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13230 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13231 if (do_inject)
13232 {
13233 /* DR 1207: 'this' is in scope in the trailing return type. */
13234 inject_this_parameter (this_type, cp_type_quals (this_type));
13235 }
13236
13237 /* Substitute the return type. */
13238 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13239
13240 if (do_inject)
13241 {
13242 current_class_ptr = save_ccp;
13243 current_class_ref = save_ccr;
13244 }
13245 }
13246 else
13247 /* Substitute the return type. */
13248 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13249
13250 if (return_type == error_mark_node)
13251 return error_mark_node;
13252 /* DR 486 clarifies that creation of a function type with an
13253 invalid return type is a deduction failure. */
13254 if (TREE_CODE (return_type) == ARRAY_TYPE
13255 || TREE_CODE (return_type) == FUNCTION_TYPE)
13256 {
13257 if (complain & tf_error)
13258 {
13259 if (TREE_CODE (return_type) == ARRAY_TYPE)
13260 error ("function returning an array");
13261 else
13262 error ("function returning a function");
13263 }
13264 return error_mark_node;
13265 }
13266 /* And DR 657. */
13267 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13268 return error_mark_node;
13269
13270 if (!late_return_type_p)
13271 {
13272 /* Substitute the argument types. */
13273 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13274 complain, in_decl);
13275 if (arg_types == error_mark_node)
13276 return error_mark_node;
13277 }
13278
13279 /* Construct a new type node and return it. */
13280 if (TREE_CODE (t) == FUNCTION_TYPE)
13281 {
13282 fntype = build_function_type (return_type, arg_types);
13283 fntype = apply_memfn_quals (fntype,
13284 type_memfn_quals (t),
13285 type_memfn_rqual (t));
13286 }
13287 else
13288 {
13289 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13290 /* Don't pick up extra function qualifiers from the basetype. */
13291 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13292 if (! MAYBE_CLASS_TYPE_P (r))
13293 {
13294 /* [temp.deduct]
13295
13296 Type deduction may fail for any of the following
13297 reasons:
13298
13299 -- Attempting to create "pointer to member of T" when T
13300 is not a class type. */
13301 if (complain & tf_error)
13302 error ("creating pointer to member function of non-class type %qT",
13303 r);
13304 return error_mark_node;
13305 }
13306
13307 fntype = build_method_type_directly (r, return_type,
13308 TREE_CHAIN (arg_types));
13309 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13310 }
13311 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13312
13313 if (late_return_type_p)
13314 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13315
13316 return fntype;
13317}
13318
13319/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13320 ARGS into that specification, and return the substituted
13321 specification. If there is no specification, return NULL_TREE. */
13322
13323static tree
13324tsubst_exception_specification (tree fntype,
13325 tree args,
13326 tsubst_flags_t complain,
13327 tree in_decl,
13328 bool defer_ok)
13329{
13330 tree specs;
13331 tree new_specs;
13332
13333 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13334 new_specs = NULL_TREE;
13335 if (specs && TREE_PURPOSE (specs))
13336 {
13337 /* A noexcept-specifier. */
13338 tree expr = TREE_PURPOSE (specs);
13339 if (TREE_CODE (expr) == INTEGER_CST)
13340 new_specs = expr;
13341 else if (defer_ok)
13342 {
13343 /* Defer instantiation of noexcept-specifiers to avoid
13344 excessive instantiations (c++/49107). */
13345 new_specs = make_node (DEFERRED_NOEXCEPT);
13346 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13347 {
13348 /* We already partially instantiated this member template,
13349 so combine the new args with the old. */
13350 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13351 = DEFERRED_NOEXCEPT_PATTERN (expr);
13352 DEFERRED_NOEXCEPT_ARGS (new_specs)
13353 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13354 }
13355 else
13356 {
13357 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13358 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13359 }
13360 }
13361 else
13362 new_specs = tsubst_copy_and_build
13363 (expr, args, complain, in_decl, /*function_p=*/false,
13364 /*integral_constant_expression_p=*/true);
13365 new_specs = build_noexcept_spec (new_specs, complain);
13366 }
13367 else if (specs)
13368 {
13369 if (! TREE_VALUE (specs))
13370 new_specs = specs;
13371 else
13372 while (specs)
13373 {
13374 tree spec;
13375 int i, len = 1;
13376 tree expanded_specs = NULL_TREE;
13377
13378 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13379 {
13380 /* Expand the pack expansion type. */
13381 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13382 args, complain,
13383 in_decl);
13384
13385 if (expanded_specs == error_mark_node)
13386 return error_mark_node;
13387 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13388 len = TREE_VEC_LENGTH (expanded_specs);
13389 else
13390 {
13391 /* We're substituting into a member template, so
13392 we got a TYPE_PACK_EXPANSION back. Add that
13393 expansion and move on. */
13394 gcc_assert (TREE_CODE (expanded_specs)
13395 == TYPE_PACK_EXPANSION);
13396 new_specs = add_exception_specifier (new_specs,
13397 expanded_specs,
13398 complain);
13399 specs = TREE_CHAIN (specs);
13400 continue;
13401 }
13402 }
13403
13404 for (i = 0; i < len; ++i)
13405 {
13406 if (expanded_specs)
13407 spec = TREE_VEC_ELT (expanded_specs, i);
13408 else
13409 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13410 if (spec == error_mark_node)
13411 return spec;
13412 new_specs = add_exception_specifier (new_specs, spec,
13413 complain);
13414 }
13415
13416 specs = TREE_CHAIN (specs);
13417 }
13418 }
13419 return new_specs;
13420}
13421
13422/* Take the tree structure T and replace template parameters used
13423 therein with the argument vector ARGS. IN_DECL is an associated
13424 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13425 Issue error and warning messages under control of COMPLAIN. Note
13426 that we must be relatively non-tolerant of extensions here, in
13427 order to preserve conformance; if we allow substitutions that
13428 should not be allowed, we may allow argument deductions that should
13429 not succeed, and therefore report ambiguous overload situations
13430 where there are none. In theory, we could allow the substitution,
13431 but indicate that it should have failed, and allow our caller to
13432 make sure that the right thing happens, but we don't try to do this
13433 yet.
13434
13435 This function is used for dealing with types, decls and the like;
13436 for expressions, use tsubst_expr or tsubst_copy. */
13437
13438tree
13439tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13440{
13441 enum tree_code code;
13442 tree type, r = NULL_TREE;
13443
13444 if (t == NULL_TREE || t == error_mark_node
13445 || t == integer_type_node
13446 || t == void_type_node
13447 || t == char_type_node
13448 || t == unknown_type_node
13449 || TREE_CODE (t) == NAMESPACE_DECL
13450 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13451 return t;
13452
13453 if (DECL_P (t))
13454 return tsubst_decl (t, args, complain);
13455
13456 if (args == NULL_TREE)
13457 return t;
13458
13459 code = TREE_CODE (t);
13460
13461 if (code == IDENTIFIER_NODE)
13462 type = IDENTIFIER_TYPE_VALUE (t);
13463 else
13464 type = TREE_TYPE (t);
13465
13466 gcc_assert (type != unknown_type_node);
13467
13468 /* Reuse typedefs. We need to do this to handle dependent attributes,
13469 such as attribute aligned. */
13470 if (TYPE_P (t)
13471 && typedef_variant_p (t))
13472 {
13473 tree decl = TYPE_NAME (t);
13474
13475 if (alias_template_specialization_p (t))
13476 {
13477 /* DECL represents an alias template and we want to
13478 instantiate it. */
13479 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13480 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13481 r = instantiate_alias_template (tmpl, gen_args, complain);
13482 }
13483 else if (DECL_CLASS_SCOPE_P (decl)
13484 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13485 && uses_template_parms (DECL_CONTEXT (decl)))
13486 {
13487 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13488 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13489 r = retrieve_specialization (tmpl, gen_args, 0);
13490 }
13491 else if (DECL_FUNCTION_SCOPE_P (decl)
13492 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13493 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13494 r = retrieve_local_specialization (decl);
13495 else
13496 /* The typedef is from a non-template context. */
13497 return t;
13498
13499 if (r)
13500 {
13501 r = TREE_TYPE (r);
13502 r = cp_build_qualified_type_real
13503 (r, cp_type_quals (t) | cp_type_quals (r),
13504 complain | tf_ignore_bad_quals);
13505 return r;
13506 }
13507 else
13508 {
13509 /* We don't have an instantiation yet, so drop the typedef. */
13510 int quals = cp_type_quals (t);
13511 t = DECL_ORIGINAL_TYPE (decl);
13512 t = cp_build_qualified_type_real (t, quals,
13513 complain | tf_ignore_bad_quals);
13514 }
13515 }
13516
13517 bool fndecl_type = (complain & tf_fndecl_type);
13518 complain &= ~tf_fndecl_type;
13519
13520 if (type
13521 && code != TYPENAME_TYPE
13522 && code != TEMPLATE_TYPE_PARM
13523 && code != TEMPLATE_PARM_INDEX
13524 && code != IDENTIFIER_NODE
13525 && code != FUNCTION_TYPE
13526 && code != METHOD_TYPE)
13527 type = tsubst (type, args, complain, in_decl);
13528 if (type == error_mark_node)
13529 return error_mark_node;
13530
13531 switch (code)
13532 {
13533 case RECORD_TYPE:
13534 case UNION_TYPE:
13535 case ENUMERAL_TYPE:
13536 return tsubst_aggr_type (t, args, complain, in_decl,
13537 /*entering_scope=*/0);
13538
13539 case ERROR_MARK:
13540 case IDENTIFIER_NODE:
13541 case VOID_TYPE:
13542 case REAL_TYPE:
13543 case COMPLEX_TYPE:
13544 case VECTOR_TYPE:
13545 case BOOLEAN_TYPE:
13546 case NULLPTR_TYPE:
13547 case LANG_TYPE:
13548 return t;
13549
13550 case INTEGER_TYPE:
13551 if (t == integer_type_node)
13552 return t;
13553
13554 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13555 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13556 return t;
13557
13558 {
13559 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13560
13561 max = tsubst_expr (omax, args, complain, in_decl,
13562 /*integral_constant_expression_p=*/false);
13563
13564 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13565 needed. */
13566 if (TREE_CODE (max) == NOP_EXPR
13567 && TREE_SIDE_EFFECTS (omax)
13568 && !TREE_TYPE (max))
13569 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13570
13571 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13572 with TREE_SIDE_EFFECTS that indicates this is not an integral
13573 constant expression. */
13574 if (processing_template_decl
13575 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13576 {
13577 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13578 TREE_SIDE_EFFECTS (max) = 1;
13579 }
13580
13581 return compute_array_index_type (NULL_TREE, max, complain);
13582 }
13583
13584 case TEMPLATE_TYPE_PARM:
13585 case TEMPLATE_TEMPLATE_PARM:
13586 case BOUND_TEMPLATE_TEMPLATE_PARM:
13587 case TEMPLATE_PARM_INDEX:
13588 {
13589 int idx;
13590 int level;
13591 int levels;
13592 tree arg = NULL_TREE;
13593
13594 /* Early in template argument deduction substitution, we don't
13595 want to reduce the level of 'auto', or it will be confused
13596 with a normal template parm in subsequent deduction. */
13597 if (is_auto (t) && (complain & tf_partial))
13598 return t;
13599
13600 r = NULL_TREE;
13601
13602 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13603 template_parm_level_and_index (t, &level, &idx);
13604
13605 levels = TMPL_ARGS_DEPTH (args);
13606 if (level <= levels
13607 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13608 {
13609 arg = TMPL_ARG (args, level, idx);
13610
13611 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13612 {
13613 /* See through ARGUMENT_PACK_SELECT arguments. */
13614 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13615 /* If the selected argument is an expansion E, that most
13616 likely means we were called from
13617 gen_elem_of_pack_expansion_instantiation during the
13618 substituting of pack an argument pack (which Ith
13619 element is a pack expansion, where I is
13620 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13621 In this case, the Ith element resulting from this
13622 substituting is going to be a pack expansion, which
13623 pattern is the pattern of E. Let's return the
13624 pattern of E, and
13625 gen_elem_of_pack_expansion_instantiation will
13626 build the resulting pack expansion from it. */
13627 if (PACK_EXPANSION_P (arg))
13628 {
13629 /* Make sure we aren't throwing away arg info. */
13630 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13631 arg = PACK_EXPANSION_PATTERN (arg);
13632 }
13633 }
13634 }
13635
13636 if (arg == error_mark_node)
13637 return error_mark_node;
13638 else if (arg != NULL_TREE)
13639 {
13640 if (ARGUMENT_PACK_P (arg))
13641 /* If ARG is an argument pack, we don't actually want to
13642 perform a substitution here, because substitutions
13643 for argument packs are only done
13644 element-by-element. We can get to this point when
13645 substituting the type of a non-type template
13646 parameter pack, when that type actually contains
13647 template parameter packs from an outer template, e.g.,
13648
13649 template<typename... Types> struct A {
13650 template<Types... Values> struct B { };
13651 }; */
13652 return t;
13653
13654 if (code == TEMPLATE_TYPE_PARM)
13655 {
13656 int quals;
13657 gcc_assert (TYPE_P (arg));
13658
13659 quals = cp_type_quals (arg) | cp_type_quals (t);
13660
13661 return cp_build_qualified_type_real
13662 (arg, quals, complain | tf_ignore_bad_quals);
13663 }
13664 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13665 {
13666 /* We are processing a type constructed from a
13667 template template parameter. */
13668 tree argvec = tsubst (TYPE_TI_ARGS (t),
13669 args, complain, in_decl);
13670 if (argvec == error_mark_node)
13671 return error_mark_node;
13672
13673 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13674 || TREE_CODE (arg) == TEMPLATE_DECL
13675 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13676
13677 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13678 /* Consider this code:
13679
13680 template <template <class> class Template>
13681 struct Internal {
13682 template <class Arg> using Bind = Template<Arg>;
13683 };
13684
13685 template <template <class> class Template, class Arg>
13686 using Instantiate = Template<Arg>; //#0
13687
13688 template <template <class> class Template,
13689 class Argument>
13690 using Bind =
13691 Instantiate<Internal<Template>::template Bind,
13692 Argument>; //#1
13693
13694 When #1 is parsed, the
13695 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13696 parameter `Template' in #0 matches the
13697 UNBOUND_CLASS_TEMPLATE representing the argument
13698 `Internal<Template>::template Bind'; We then want
13699 to assemble the type `Bind<Argument>' that can't
13700 be fully created right now, because
13701 `Internal<Template>' not being complete, the Bind
13702 template cannot be looked up in that context. So
13703 we need to "store" `Bind<Argument>' for later
13704 when the context of Bind becomes complete. Let's
13705 store that in a TYPENAME_TYPE. */
13706 return make_typename_type (TYPE_CONTEXT (arg),
13707 build_nt (TEMPLATE_ID_EXPR,
13708 TYPE_IDENTIFIER (arg),
13709 argvec),
13710 typename_type,
13711 complain);
13712
13713 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13714 are resolving nested-types in the signature of a
13715 member function templates. Otherwise ARG is a
13716 TEMPLATE_DECL and is the real template to be
13717 instantiated. */
13718 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13719 arg = TYPE_NAME (arg);
13720
13721 r = lookup_template_class (arg,
13722 argvec, in_decl,
13723 DECL_CONTEXT (arg),
13724 /*entering_scope=*/0,
13725 complain);
13726 return cp_build_qualified_type_real
13727 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13728 }
13729 else if (code == TEMPLATE_TEMPLATE_PARM)
13730 return arg;
13731 else
13732 /* TEMPLATE_PARM_INDEX. */
13733 return convert_from_reference (unshare_expr (arg));
13734 }
13735
13736 if (level == 1)
13737 /* This can happen during the attempted tsubst'ing in
13738 unify. This means that we don't yet have any information
13739 about the template parameter in question. */
13740 return t;
13741
13742 /* If we get here, we must have been looking at a parm for a
13743 more deeply nested template. Make a new version of this
13744 template parameter, but with a lower level. */
13745 switch (code)
13746 {
13747 case TEMPLATE_TYPE_PARM:
13748 case TEMPLATE_TEMPLATE_PARM:
13749 case BOUND_TEMPLATE_TEMPLATE_PARM:
13750 if (cp_type_quals (t))
13751 {
13752 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13753 r = cp_build_qualified_type_real
13754 (r, cp_type_quals (t),
13755 complain | (code == TEMPLATE_TYPE_PARM
13756 ? tf_ignore_bad_quals : 0));
13757 }
13758 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13759 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13760 && (r = (TEMPLATE_PARM_DESCENDANTS
13761 (TEMPLATE_TYPE_PARM_INDEX (t))))
13762 && (r = TREE_TYPE (r))
13763 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13764 /* Break infinite recursion when substituting the constraints
13765 of a constrained placeholder. */;
13766 else
13767 {
13768 r = copy_type (t);
13769 TEMPLATE_TYPE_PARM_INDEX (r)
13770 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13771 r, levels, args, complain);
13772 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13773 TYPE_MAIN_VARIANT (r) = r;
13774 TYPE_POINTER_TO (r) = NULL_TREE;
13775 TYPE_REFERENCE_TO (r) = NULL_TREE;
13776
13777 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13778 {
13779 /* Propagate constraints on placeholders. */
13780 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13781 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13782 = tsubst_constraint (constr, args, complain, in_decl);
13783 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13784 {
13785 pl = tsubst_copy (pl, args, complain, in_decl);
13786 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13787 }
13788 }
13789
13790 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13791 /* We have reduced the level of the template
13792 template parameter, but not the levels of its
13793 template parameters, so canonical_type_parameter
13794 will not be able to find the canonical template
13795 template parameter for this level. Thus, we
13796 require structural equality checking to compare
13797 TEMPLATE_TEMPLATE_PARMs. */
13798 SET_TYPE_STRUCTURAL_EQUALITY (r);
13799 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13800 SET_TYPE_STRUCTURAL_EQUALITY (r);
13801 else
13802 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13803
13804 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13805 {
13806 tree tinfo = TYPE_TEMPLATE_INFO (t);
13807 /* We might need to substitute into the types of non-type
13808 template parameters. */
13809 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13810 complain, in_decl);
13811 if (tmpl == error_mark_node)
13812 return error_mark_node;
13813 tree argvec = tsubst (TI_ARGS (tinfo), args,
13814 complain, in_decl);
13815 if (argvec == error_mark_node)
13816 return error_mark_node;
13817
13818 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13819 = build_template_info (tmpl, argvec);
13820 }
13821 }
13822 break;
13823
13824 case TEMPLATE_PARM_INDEX:
13825 /* OK, now substitute the type of the non-type parameter. We
13826 couldn't do it earlier because it might be an auto parameter,
13827 and we wouldn't need to if we had an argument. */
13828 type = tsubst (type, args, complain, in_decl);
13829 r = reduce_template_parm_level (t, type, levels, args, complain);
13830 break;
13831
13832 default:
13833 gcc_unreachable ();
13834 }
13835
13836 return r;
13837 }
13838
13839 case TREE_LIST:
13840 {
13841 tree purpose, value, chain;
13842
13843 if (t == void_list_node)
13844 return t;
13845
13846 purpose = TREE_PURPOSE (t);
13847 if (purpose)
13848 {
13849 purpose = tsubst (purpose, args, complain, in_decl);
13850 if (purpose == error_mark_node)
13851 return error_mark_node;
13852 }
13853 value = TREE_VALUE (t);
13854 if (value)
13855 {
13856 value = tsubst (value, args, complain, in_decl);
13857 if (value == error_mark_node)
13858 return error_mark_node;
13859 }
13860 chain = TREE_CHAIN (t);
13861 if (chain && chain != void_type_node)
13862 {
13863 chain = tsubst (chain, args, complain, in_decl);
13864 if (chain == error_mark_node)
13865 return error_mark_node;
13866 }
13867 if (purpose == TREE_PURPOSE (t)
13868 && value == TREE_VALUE (t)
13869 && chain == TREE_CHAIN (t))
13870 return t;
13871 return hash_tree_cons (purpose, value, chain);
13872 }
13873
13874 case TREE_BINFO:
13875 /* We should never be tsubsting a binfo. */
13876 gcc_unreachable ();
13877
13878 case TREE_VEC:
13879 /* A vector of template arguments. */
13880 gcc_assert (!type);
13881 return tsubst_template_args (t, args, complain, in_decl);
13882
13883 case POINTER_TYPE:
13884 case REFERENCE_TYPE:
13885 {
13886 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13887 return t;
13888
13889 /* [temp.deduct]
13890
13891 Type deduction may fail for any of the following
13892 reasons:
13893
13894 -- Attempting to create a pointer to reference type.
13895 -- Attempting to create a reference to a reference type or
13896 a reference to void.
13897
13898 Core issue 106 says that creating a reference to a reference
13899 during instantiation is no longer a cause for failure. We
13900 only enforce this check in strict C++98 mode. */
13901 if ((TREE_CODE (type) == REFERENCE_TYPE
13902 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13903 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13904 {
13905 static location_t last_loc;
13906
13907 /* We keep track of the last time we issued this error
13908 message to avoid spewing a ton of messages during a
13909 single bad template instantiation. */
13910 if (complain & tf_error
13911 && last_loc != input_location)
13912 {
13913 if (VOID_TYPE_P (type))
13914 error ("forming reference to void");
13915 else if (code == POINTER_TYPE)
13916 error ("forming pointer to reference type %qT", type);
13917 else
13918 error ("forming reference to reference type %qT", type);
13919 last_loc = input_location;
13920 }
13921
13922 return error_mark_node;
13923 }
13924 else if (TREE_CODE (type) == FUNCTION_TYPE
13925 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13926 || type_memfn_rqual (type) != REF_QUAL_NONE))
13927 {
13928 if (complain & tf_error)
13929 {
13930 if (code == POINTER_TYPE)
13931 error ("forming pointer to qualified function type %qT",
13932 type);
13933 else
13934 error ("forming reference to qualified function type %qT",
13935 type);
13936 }
13937 return error_mark_node;
13938 }
13939 else if (code == POINTER_TYPE)
13940 {
13941 r = build_pointer_type (type);
13942 if (TREE_CODE (type) == METHOD_TYPE)
13943 r = build_ptrmemfunc_type (r);
13944 }
13945 else if (TREE_CODE (type) == REFERENCE_TYPE)
13946 /* In C++0x, during template argument substitution, when there is an
13947 attempt to create a reference to a reference type, reference
13948 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13949
13950 "If a template-argument for a template-parameter T names a type
13951 that is a reference to a type A, an attempt to create the type
13952 'lvalue reference to cv T' creates the type 'lvalue reference to
13953 A,' while an attempt to create the type type rvalue reference to
13954 cv T' creates the type T"
13955 */
13956 r = cp_build_reference_type
13957 (TREE_TYPE (type),
13958 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13959 else
13960 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13961 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13962
13963 if (r != error_mark_node)
13964 /* Will this ever be needed for TYPE_..._TO values? */
13965 layout_type (r);
13966
13967 return r;
13968 }
13969 case OFFSET_TYPE:
13970 {
13971 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13972 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13973 {
13974 /* [temp.deduct]
13975
13976 Type deduction may fail for any of the following
13977 reasons:
13978
13979 -- Attempting to create "pointer to member of T" when T
13980 is not a class type. */
13981 if (complain & tf_error)
13982 error ("creating pointer to member of non-class type %qT", r);
13983 return error_mark_node;
13984 }
13985 if (TREE_CODE (type) == REFERENCE_TYPE)
13986 {
13987 if (complain & tf_error)
13988 error ("creating pointer to member reference type %qT", type);
13989 return error_mark_node;
13990 }
13991 if (VOID_TYPE_P (type))
13992 {
13993 if (complain & tf_error)
13994 error ("creating pointer to member of type void");
13995 return error_mark_node;
13996 }
13997 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13998 if (TREE_CODE (type) == FUNCTION_TYPE)
13999 {
14000 /* The type of the implicit object parameter gets its
14001 cv-qualifiers from the FUNCTION_TYPE. */
14002 tree memptr;
14003 tree method_type
14004 = build_memfn_type (type, r, type_memfn_quals (type),
14005 type_memfn_rqual (type));
14006 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14007 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14008 complain);
14009 }
14010 else
14011 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14012 cp_type_quals (t),
14013 complain);
14014 }
14015 case FUNCTION_TYPE:
14016 case METHOD_TYPE:
14017 {
14018 tree fntype;
14019 tree specs;
14020 fntype = tsubst_function_type (t, args, complain, in_decl);
14021 if (fntype == error_mark_node)
14022 return error_mark_node;
14023
14024 /* Substitute the exception specification. */
14025 specs = tsubst_exception_specification (t, args, complain, in_decl,
14026 /*defer_ok*/fndecl_type);
14027 if (specs == error_mark_node)
14028 return error_mark_node;
14029 if (specs)
14030 fntype = build_exception_variant (fntype, specs);
14031 return fntype;
14032 }
14033 case ARRAY_TYPE:
14034 {
14035 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14036 if (domain == error_mark_node)
14037 return error_mark_node;
14038
14039 /* As an optimization, we avoid regenerating the array type if
14040 it will obviously be the same as T. */
14041 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14042 return t;
14043
14044 /* These checks should match the ones in create_array_type_for_decl.
14045
14046 [temp.deduct]
14047
14048 The deduction may fail for any of the following reasons:
14049
14050 -- Attempting to create an array with an element type that
14051 is void, a function type, or a reference type, or [DR337]
14052 an abstract class type. */
14053 if (VOID_TYPE_P (type)
14054 || TREE_CODE (type) == FUNCTION_TYPE
14055 || (TREE_CODE (type) == ARRAY_TYPE
14056 && TYPE_DOMAIN (type) == NULL_TREE)
14057 || TREE_CODE (type) == REFERENCE_TYPE)
14058 {
14059 if (complain & tf_error)
14060 error ("creating array of %qT", type);
14061 return error_mark_node;
14062 }
14063
14064 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14065 return error_mark_node;
14066
14067 r = build_cplus_array_type (type, domain);
14068
14069 if (TYPE_USER_ALIGN (t))
14070 {
14071 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14072 TYPE_USER_ALIGN (r) = 1;
14073 }
14074
14075 return r;
14076 }
14077
14078 case TYPENAME_TYPE:
14079 {
14080 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14081 in_decl, /*entering_scope=*/1);
14082 if (ctx == error_mark_node)
14083 return error_mark_node;
14084
14085 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14086 complain, in_decl);
14087 if (f == error_mark_node)
14088 return error_mark_node;
14089
14090 if (!MAYBE_CLASS_TYPE_P (ctx))
14091 {
14092 if (complain & tf_error)
14093 error ("%qT is not a class, struct, or union type", ctx);
14094 return error_mark_node;
14095 }
14096 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14097 {
14098 /* Normally, make_typename_type does not require that the CTX
14099 have complete type in order to allow things like:
14100
14101 template <class T> struct S { typename S<T>::X Y; };
14102
14103 But, such constructs have already been resolved by this
14104 point, so here CTX really should have complete type, unless
14105 it's a partial instantiation. */
14106 ctx = complete_type (ctx);
14107 if (!COMPLETE_TYPE_P (ctx))
14108 {
14109 if (complain & tf_error)
14110 cxx_incomplete_type_error (NULL_TREE, ctx);
14111 return error_mark_node;
14112 }
14113 }
14114
14115 f = make_typename_type (ctx, f, typename_type,
14116 complain | tf_keep_type_decl);
14117 if (f == error_mark_node)
14118 return f;
14119 if (TREE_CODE (f) == TYPE_DECL)
14120 {
14121 complain |= tf_ignore_bad_quals;
14122 f = TREE_TYPE (f);
14123 }
14124
14125 if (TREE_CODE (f) != TYPENAME_TYPE)
14126 {
14127 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14128 {
14129 if (complain & tf_error)
14130 error ("%qT resolves to %qT, which is not an enumeration type",
14131 t, f);
14132 else
14133 return error_mark_node;
14134 }
14135 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14136 {
14137 if (complain & tf_error)
14138 error ("%qT resolves to %qT, which is is not a class type",
14139 t, f);
14140 else
14141 return error_mark_node;
14142 }
14143 }
14144
14145 return cp_build_qualified_type_real
14146 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14147 }
14148
14149 case UNBOUND_CLASS_TEMPLATE:
14150 {
14151 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14152 in_decl, /*entering_scope=*/1);
14153 tree name = TYPE_IDENTIFIER (t);
14154 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14155
14156 if (ctx == error_mark_node || name == error_mark_node)
14157 return error_mark_node;
14158
14159 if (parm_list)
14160 parm_list = tsubst_template_parms (parm_list, args, complain);
14161 return make_unbound_class_template (ctx, name, parm_list, complain);
14162 }
14163
14164 case TYPEOF_TYPE:
14165 {
14166 tree type;
14167
14168 ++cp_unevaluated_operand;
14169 ++c_inhibit_evaluation_warnings;
14170
14171 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14172 complain, in_decl,
14173 /*integral_constant_expression_p=*/false);
14174
14175 --cp_unevaluated_operand;
14176 --c_inhibit_evaluation_warnings;
14177
14178 type = finish_typeof (type);
14179 return cp_build_qualified_type_real (type,
14180 cp_type_quals (t)
14181 | cp_type_quals (type),
14182 complain);
14183 }
14184
14185 case DECLTYPE_TYPE:
14186 {
14187 tree type;
14188
14189 ++cp_unevaluated_operand;
14190 ++c_inhibit_evaluation_warnings;
14191
14192 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14193 complain|tf_decltype, in_decl,
14194 /*function_p*/false,
14195 /*integral_constant_expression*/false);
14196
14197 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14198 {
14199 if (type == NULL_TREE)
14200 {
14201 if (complain & tf_error)
14202 error ("empty initializer in lambda init-capture");
14203 type = error_mark_node;
14204 }
14205 else if (TREE_CODE (type) == TREE_LIST)
14206 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14207 }
14208
14209 --cp_unevaluated_operand;
14210 --c_inhibit_evaluation_warnings;
14211
14212 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14213 type = lambda_capture_field_type (type,
14214 DECLTYPE_FOR_INIT_CAPTURE (t),
14215 DECLTYPE_FOR_REF_CAPTURE (t));
14216 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14217 type = lambda_proxy_type (type);
14218 else
14219 {
14220 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14221 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14222 && EXPR_P (type))
14223 /* In a template ~id could be either a complement expression
14224 or an unqualified-id naming a destructor; if instantiating
14225 it produces an expression, it's not an id-expression or
14226 member access. */
14227 id = false;
14228 type = finish_decltype_type (type, id, complain);
14229 }
14230 return cp_build_qualified_type_real (type,
14231 cp_type_quals (t)
14232 | cp_type_quals (type),
14233 complain | tf_ignore_bad_quals);
14234 }
14235
14236 case UNDERLYING_TYPE:
14237 {
14238 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14239 complain, in_decl);
14240 return finish_underlying_type (type);
14241 }
14242
14243 case TYPE_ARGUMENT_PACK:
14244 case NONTYPE_ARGUMENT_PACK:
14245 {
14246 tree r;
14247
14248 if (code == NONTYPE_ARGUMENT_PACK)
14249 {
14250 r = make_node (code);
14251 /* Set the already-substituted type. */
14252 TREE_TYPE (r) = type;
14253 }
14254 else
14255 r = cxx_make_type (code);
14256
14257 tree pack_args = ARGUMENT_PACK_ARGS (t);
14258 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14259 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14260
14261 return r;
14262 }
14263
14264 case VOID_CST:
14265 case INTEGER_CST:
14266 case REAL_CST:
14267 case STRING_CST:
14268 case PLUS_EXPR:
14269 case MINUS_EXPR:
14270 case NEGATE_EXPR:
14271 case NOP_EXPR:
14272 case INDIRECT_REF:
14273 case ADDR_EXPR:
14274 case CALL_EXPR:
14275 case ARRAY_REF:
14276 case SCOPE_REF:
14277 /* We should use one of the expression tsubsts for these codes. */
14278 gcc_unreachable ();
14279
14280 default:
14281 sorry ("use of %qs in template", get_tree_code_name (code));
14282 return error_mark_node;
14283 }
14284}
14285
14286/* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14287 type of the expression on the left-hand side of the "." or "->"
14288 operator. */
14289
14290static tree
14291tsubst_baselink (tree baselink, tree object_type,
14292 tree args, tsubst_flags_t complain, tree in_decl)
14293{
14294 tree name;
14295 tree qualifying_scope;
14296 tree fns;
14297 tree optype;
14298 tree template_args = 0;
14299 bool template_id_p = false;
14300 bool qualified = BASELINK_QUALIFIED_P (baselink);
14301
14302 /* A baselink indicates a function from a base class. Both the
14303 BASELINK_ACCESS_BINFO and the base class referenced may
14304 indicate bases of the template class, rather than the
14305 instantiated class. In addition, lookups that were not
14306 ambiguous before may be ambiguous now. Therefore, we perform
14307 the lookup again. */
14308 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14309 qualifying_scope = tsubst (qualifying_scope, args,
14310 complain, in_decl);
14311 fns = BASELINK_FUNCTIONS (baselink);
14312 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14313 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14314 {
14315 template_id_p = true;
14316 template_args = TREE_OPERAND (fns, 1);
14317 fns = TREE_OPERAND (fns, 0);
14318 if (template_args)
14319 template_args = tsubst_template_args (template_args, args,
14320 complain, in_decl);
14321 }
14322 name = DECL_NAME (get_first_fn (fns));
14323 if (IDENTIFIER_TYPENAME_P (name))
14324 name = mangle_conv_op_name_for_type (optype);
14325 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14326 if (!baselink)
14327 {
14328 if (constructor_name_p (name, qualifying_scope))
14329 {
14330 if (complain & tf_error)
14331 error ("cannot call constructor %<%T::%D%> directly",
14332 qualifying_scope, name);
14333 }
14334 return error_mark_node;
14335 }
14336
14337 /* If lookup found a single function, mark it as used at this
14338 point. (If it lookup found multiple functions the one selected
14339 later by overload resolution will be marked as used at that
14340 point.) */
14341 if (BASELINK_P (baselink))
14342 fns = BASELINK_FUNCTIONS (baselink);
14343 if (!template_id_p && !really_overloaded_fn (fns)
14344 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14345 return error_mark_node;
14346
14347 /* Add back the template arguments, if present. */
14348 if (BASELINK_P (baselink) && template_id_p)
14349 BASELINK_FUNCTIONS (baselink)
14350 = build2 (TEMPLATE_ID_EXPR,
14351 unknown_type_node,
14352 BASELINK_FUNCTIONS (baselink),
14353 template_args);
14354 /* Update the conversion operator type. */
14355 if (BASELINK_P (baselink))
14356 BASELINK_OPTYPE (baselink) = optype;
14357
14358 if (!object_type)
14359 object_type = current_class_type;
14360
14361 if (qualified || name == complete_dtor_identifier)
14362 {
14363 baselink = adjust_result_of_qualified_name_lookup (baselink,
14364 qualifying_scope,
14365 object_type);
14366 if (!qualified)
14367 /* We need to call adjust_result_of_qualified_name_lookup in case the
14368 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14369 so that we still get virtual function binding. */
14370 BASELINK_QUALIFIED_P (baselink) = false;
14371 }
14372 return baselink;
14373}
14374
14375/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14376 true if the qualified-id will be a postfix-expression in-and-of
14377 itself; false if more of the postfix-expression follows the
14378 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14379 of "&". */
14380
14381static tree
14382tsubst_qualified_id (tree qualified_id, tree args,
14383 tsubst_flags_t complain, tree in_decl,
14384 bool done, bool address_p)
14385{
14386 tree expr;
14387 tree scope;
14388 tree name;
14389 bool is_template;
14390 tree template_args;
14391 location_t loc = UNKNOWN_LOCATION;
14392
14393 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14394
14395 /* Figure out what name to look up. */
14396 name = TREE_OPERAND (qualified_id, 1);
14397 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14398 {
14399 is_template = true;
14400 loc = EXPR_LOCATION (name);
14401 template_args = TREE_OPERAND (name, 1);
14402 if (template_args)
14403 template_args = tsubst_template_args (template_args, args,
14404 complain, in_decl);
14405 if (template_args == error_mark_node)
14406 return error_mark_node;
14407 name = TREE_OPERAND (name, 0);
14408 }
14409 else
14410 {
14411 is_template = false;
14412 template_args = NULL_TREE;
14413 }
14414
14415 /* Substitute into the qualifying scope. When there are no ARGS, we
14416 are just trying to simplify a non-dependent expression. In that
14417 case the qualifying scope may be dependent, and, in any case,
14418 substituting will not help. */
14419 scope = TREE_OPERAND (qualified_id, 0);
14420 if (args)
14421 {
14422 scope = tsubst (scope, args, complain, in_decl);
14423 expr = tsubst_copy (name, args, complain, in_decl);
14424 }
14425 else
14426 expr = name;
14427
14428 if (dependent_scope_p (scope))
14429 {
14430 if (is_template)
14431 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14432 tree r = build_qualified_name (NULL_TREE, scope, expr,
14433 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14434 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14435 return r;
14436 }
14437
14438 if (!BASELINK_P (name) && !DECL_P (expr))
14439 {
14440 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14441 {
14442 /* A BIT_NOT_EXPR is used to represent a destructor. */
14443 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14444 {
14445 error ("qualifying type %qT does not match destructor name ~%qT",
14446 scope, TREE_OPERAND (expr, 0));
14447 expr = error_mark_node;
14448 }
14449 else
14450 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14451 /*is_type_p=*/0, false);
14452 }
14453 else
14454 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14455 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14456 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14457 {
14458 if (complain & tf_error)
14459 {
14460 error ("dependent-name %qE is parsed as a non-type, but "
14461 "instantiation yields a type", qualified_id);
14462 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14463 }
14464 return error_mark_node;
14465 }
14466 }
14467
14468 if (DECL_P (expr))
14469 {
14470 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14471 scope);
14472 /* Remember that there was a reference to this entity. */
14473 if (!mark_used (expr, complain) && !(complain & tf_error))
14474 return error_mark_node;
14475 }
14476
14477 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14478 {
14479 if (complain & tf_error)
14480 qualified_name_lookup_error (scope,
14481 TREE_OPERAND (qualified_id, 1),
14482 expr, input_location);
14483 return error_mark_node;
14484 }
14485
14486 if (is_template)
14487 {
14488 if (variable_template_p (expr))
14489 expr = lookup_and_finish_template_variable (expr, template_args,
14490 complain);
14491 else
14492 expr = lookup_template_function (expr, template_args);
14493 }
14494
14495 if (expr == error_mark_node && complain & tf_error)
14496 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14497 expr, input_location);
14498 else if (TYPE_P (scope))
14499 {
14500 expr = (adjust_result_of_qualified_name_lookup
14501 (expr, scope, current_nonlambda_class_type ()));
14502 expr = (finish_qualified_id_expr
14503 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14504 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14505 /*template_arg_p=*/false, complain));
14506 }
14507
14508 /* Expressions do not generally have reference type. */
14509 if (TREE_CODE (expr) != SCOPE_REF
14510 /* However, if we're about to form a pointer-to-member, we just
14511 want the referenced member referenced. */
14512 && TREE_CODE (expr) != OFFSET_REF)
14513 expr = convert_from_reference (expr);
14514
14515 if (REF_PARENTHESIZED_P (qualified_id))
14516 expr = force_paren_expr (expr);
14517
14518 return expr;
14519}
14520
14521/* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14522 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14523 for tsubst. */
14524
14525static tree
14526tsubst_init (tree init, tree decl, tree args,
14527 tsubst_flags_t complain, tree in_decl)
14528{
14529 if (!init)
14530 return NULL_TREE;
14531
14532 init = tsubst_expr (init, args, complain, in_decl, false);
14533
14534 if (!init && TREE_TYPE (decl) != error_mark_node)
14535 {
14536 /* If we had an initializer but it
14537 instantiated to nothing,
14538 value-initialize the object. This will
14539 only occur when the initializer was a
14540 pack expansion where the parameter packs
14541 used in that expansion were of length
14542 zero. */
14543 init = build_value_init (TREE_TYPE (decl),
14544 complain);
14545 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14546 init = get_target_expr_sfinae (init, complain);
14547 if (TREE_CODE (init) == TARGET_EXPR)
14548 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14549 }
14550
14551 return init;
14552}
14553
14554/* Like tsubst, but deals with expressions. This function just replaces
14555 template parms; to finish processing the resultant expression, use
14556 tsubst_copy_and_build or tsubst_expr. */
14557
14558static tree
14559tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14560{
14561 enum tree_code code;
14562 tree r;
14563
14564 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14565 return t;
14566
14567 code = TREE_CODE (t);
14568
14569 switch (code)
14570 {
14571 case PARM_DECL:
14572 r = retrieve_local_specialization (t);
14573
14574 if (r == NULL_TREE)
14575 {
14576 /* We get here for a use of 'this' in an NSDMI as part of a
14577 constructor call or as part of an aggregate initialization. */
14578 if (DECL_NAME (t) == this_identifier
14579 && ((current_function_decl
14580 && DECL_CONSTRUCTOR_P (current_function_decl))
14581 || (current_class_ref
14582 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14583 return current_class_ptr;
14584
14585 /* This can happen for a parameter name used later in a function
14586 declaration (such as in a late-specified return type). Just
14587 make a dummy decl, since it's only used for its type. */
14588 gcc_assert (cp_unevaluated_operand != 0);
14589 r = tsubst_decl (t, args, complain);
14590 /* Give it the template pattern as its context; its true context
14591 hasn't been instantiated yet and this is good enough for
14592 mangling. */
14593 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14594 }
14595
14596 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14597 r = ARGUMENT_PACK_SELECT_ARG (r);
14598 if (!mark_used (r, complain) && !(complain & tf_error))
14599 return error_mark_node;
14600 return r;
14601
14602 case CONST_DECL:
14603 {
14604 tree enum_type;
14605 tree v;
14606
14607 if (DECL_TEMPLATE_PARM_P (t))
14608 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14609 /* There is no need to substitute into namespace-scope
14610 enumerators. */
14611 if (DECL_NAMESPACE_SCOPE_P (t))
14612 return t;
14613 /* If ARGS is NULL, then T is known to be non-dependent. */
14614 if (args == NULL_TREE)
14615 return scalar_constant_value (t);
14616
14617 /* Unfortunately, we cannot just call lookup_name here.
14618 Consider:
14619
14620 template <int I> int f() {
14621 enum E { a = I };
14622 struct S { void g() { E e = a; } };
14623 };
14624
14625 When we instantiate f<7>::S::g(), say, lookup_name is not
14626 clever enough to find f<7>::a. */
14627 enum_type
14628 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14629 /*entering_scope=*/0);
14630
14631 for (v = TYPE_VALUES (enum_type);
14632 v != NULL_TREE;
14633 v = TREE_CHAIN (v))
14634 if (TREE_PURPOSE (v) == DECL_NAME (t))
14635 return TREE_VALUE (v);
14636
14637 /* We didn't find the name. That should never happen; if
14638 name-lookup found it during preliminary parsing, we
14639 should find it again here during instantiation. */
14640 gcc_unreachable ();
14641 }
14642 return t;
14643
14644 case FIELD_DECL:
14645 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14646 {
14647 /* Check for a local specialization set up by
14648 tsubst_pack_expansion. */
14649 if (tree r = retrieve_local_specialization (t))
14650 {
14651 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14652 r = ARGUMENT_PACK_SELECT_ARG (r);
14653 return r;
14654 }
14655
14656 /* When retrieving a capture pack from a generic lambda, remove the
14657 lambda call op's own template argument list from ARGS. Only the
14658 template arguments active for the closure type should be used to
14659 retrieve the pack specialization. */
14660 if (LAMBDA_FUNCTION_P (current_function_decl)
14661 && (template_class_depth (DECL_CONTEXT (t))
14662 != TMPL_ARGS_DEPTH (args)))
14663 args = strip_innermost_template_args (args, 1);
14664
14665 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14666 tsubst_decl put in the hash table. */
14667 return retrieve_specialization (t, args, 0);
14668 }
14669
14670 if (DECL_CONTEXT (t))
14671 {
14672 tree ctx;
14673
14674 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14675 /*entering_scope=*/1);
14676 if (ctx != DECL_CONTEXT (t))
14677 {
14678 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14679 if (!r)
14680 {
14681 if (complain & tf_error)
14682 error ("using invalid field %qD", t);
14683 return error_mark_node;
14684 }
14685 return r;
14686 }
14687 }
14688
14689 return t;
14690
14691 case VAR_DECL:
14692 case FUNCTION_DECL:
14693 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14694 r = tsubst (t, args, complain, in_decl);
14695 else if (local_variable_p (t)
14696 && uses_template_parms (DECL_CONTEXT (t)))
14697 {
14698 r = retrieve_local_specialization (t);
14699 if (r == NULL_TREE)
14700 {
14701 /* First try name lookup to find the instantiation. */
14702 r = lookup_name (DECL_NAME (t));
14703 if (r && !is_capture_proxy (r))
14704 {
14705 /* Make sure that the one we found is the one we want. */
14706 tree ctx = DECL_CONTEXT (t);
14707 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14708 ctx = tsubst (ctx, args, complain, in_decl);
14709 if (ctx != DECL_CONTEXT (r))
14710 r = NULL_TREE;
14711 }
14712
14713 if (r)
14714 /* OK */;
14715 else
14716 {
14717 /* This can happen for a variable used in a
14718 late-specified return type of a local lambda, or for a
14719 local static or constant. Building a new VAR_DECL
14720 should be OK in all those cases. */
14721 r = tsubst_decl (t, args, complain);
14722 if (local_specializations)
14723 /* Avoid infinite recursion (79640). */
14724 register_local_specialization (r, t);
14725 if (decl_maybe_constant_var_p (r))
14726 {
14727 /* We can't call cp_finish_decl, so handle the
14728 initializer by hand. */
14729 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14730 complain, in_decl);
14731 if (!processing_template_decl)
14732 init = maybe_constant_init (init);
14733 if (processing_template_decl
14734 ? potential_constant_expression (init)
14735 : reduced_constant_expression_p (init))
14736 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14737 = TREE_CONSTANT (r) = true;
14738 DECL_INITIAL (r) = init;
14739 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14740 TREE_TYPE (r)
14741 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14742 complain, adc_variable_type);
14743 }
14744 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14745 || decl_constant_var_p (r)
14746 || errorcount || sorrycount);
14747 if (!processing_template_decl
14748 && !TREE_STATIC (r))
14749 r = process_outer_var_ref (r, complain);
14750 }
14751 /* Remember this for subsequent uses. */
14752 if (local_specializations)
14753 register_local_specialization (r, t);
14754 }
14755 }
14756 else
14757 r = t;
14758 if (!mark_used (r, complain))
14759 return error_mark_node;
14760 return r;
14761
14762 case NAMESPACE_DECL:
14763 return t;
14764
14765 case OVERLOAD:
14766 /* An OVERLOAD will always be a non-dependent overload set; an
14767 overload set from function scope will just be represented with an
14768 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14769 gcc_assert (!uses_template_parms (t));
14770 return t;
14771
14772 case BASELINK:
14773 return tsubst_baselink (t, current_nonlambda_class_type (),
14774 args, complain, in_decl);
14775
14776 case TEMPLATE_DECL:
14777 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14778 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14779 args, complain, in_decl);
14780 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14781 return tsubst (t, args, complain, in_decl);
14782 else if (DECL_CLASS_SCOPE_P (t)
14783 && uses_template_parms (DECL_CONTEXT (t)))
14784 {
14785 /* Template template argument like the following example need
14786 special treatment:
14787
14788 template <template <class> class TT> struct C {};
14789 template <class T> struct D {
14790 template <class U> struct E {};
14791 C<E> c; // #1
14792 };
14793 D<int> d; // #2
14794
14795 We are processing the template argument `E' in #1 for
14796 the template instantiation #2. Originally, `E' is a
14797 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14798 have to substitute this with one having context `D<int>'. */
14799
14800 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14801 if (dependent_scope_p (context))
14802 {
14803 /* When rewriting a constructor into a deduction guide, a
14804 non-dependent name can become dependent, so memtmpl<args>
14805 becomes context::template memtmpl<args>. */
14806 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14807 return build_qualified_name (type, context, DECL_NAME (t),
14808 /*template*/true);
14809 }
14810 return lookup_field (context, DECL_NAME(t), 0, false);
14811 }
14812 else
14813 /* Ordinary template template argument. */
14814 return t;
14815
14816 case CAST_EXPR:
14817 case REINTERPRET_CAST_EXPR:
14818 case CONST_CAST_EXPR:
14819 case STATIC_CAST_EXPR:
14820 case DYNAMIC_CAST_EXPR:
14821 case IMPLICIT_CONV_EXPR:
14822 case CONVERT_EXPR:
14823 case NOP_EXPR:
14824 {
14825 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14826 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14827 return build1 (code, type, op0);
14828 }
14829
14830 case SIZEOF_EXPR:
14831 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14832 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14833 {
14834 tree expanded, op = TREE_OPERAND (t, 0);
14835 int len = 0;
14836
14837 if (SIZEOF_EXPR_TYPE_P (t))
14838 op = TREE_TYPE (op);
14839
14840 ++cp_unevaluated_operand;
14841 ++c_inhibit_evaluation_warnings;
14842 /* We only want to compute the number of arguments. */
14843 if (PACK_EXPANSION_P (op))
14844 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14845 else
14846 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14847 args, complain, in_decl);
14848 --cp_unevaluated_operand;
14849 --c_inhibit_evaluation_warnings;
14850
14851 if (TREE_CODE (expanded) == TREE_VEC)
14852 {
14853 len = TREE_VEC_LENGTH (expanded);
14854 /* Set TREE_USED for the benefit of -Wunused. */
14855 for (int i = 0; i < len; i++)
14856 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14857 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14858 }
14859
14860 if (expanded == error_mark_node)
14861 return error_mark_node;
14862 else if (PACK_EXPANSION_P (expanded)
14863 || (TREE_CODE (expanded) == TREE_VEC
14864 && pack_expansion_args_count (expanded)))
14865
14866 {
14867 if (PACK_EXPANSION_P (expanded))
14868 /* OK. */;
14869 else if (TREE_VEC_LENGTH (expanded) == 1)
14870 expanded = TREE_VEC_ELT (expanded, 0);
14871 else
14872 expanded = make_argument_pack (expanded);
14873
14874 if (TYPE_P (expanded))
14875 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14876 complain & tf_error);
14877 else
14878 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14879 complain & tf_error);
14880 }
14881 else
14882 return build_int_cst (size_type_node, len);
14883 }
14884 if (SIZEOF_EXPR_TYPE_P (t))
14885 {
14886 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14887 args, complain, in_decl);
14888 r = build1 (NOP_EXPR, r, error_mark_node);
14889 r = build1 (SIZEOF_EXPR,
14890 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14891 SIZEOF_EXPR_TYPE_P (r) = 1;
14892 return r;
14893 }
14894 /* Fall through */
14895
14896 case INDIRECT_REF:
14897 case NEGATE_EXPR:
14898 case TRUTH_NOT_EXPR:
14899 case BIT_NOT_EXPR:
14900 case ADDR_EXPR:
14901 case UNARY_PLUS_EXPR: /* Unary + */
14902 case ALIGNOF_EXPR:
14903 case AT_ENCODE_EXPR:
14904 case ARROW_EXPR:
14905 case THROW_EXPR:
14906 case TYPEID_EXPR:
14907 case REALPART_EXPR:
14908 case IMAGPART_EXPR:
14909 case PAREN_EXPR:
14910 {
14911 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14912 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14913 return build1 (code, type, op0);
14914 }
14915
14916 case COMPONENT_REF:
14917 {
14918 tree object;
14919 tree name;
14920
14921 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14922 name = TREE_OPERAND (t, 1);
14923 if (TREE_CODE (name) == BIT_NOT_EXPR)
14924 {
14925 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14926 complain, in_decl);
14927 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14928 }
14929 else if (TREE_CODE (name) == SCOPE_REF
14930 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14931 {
14932 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14933 complain, in_decl);
14934 name = TREE_OPERAND (name, 1);
14935 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14936 complain, in_decl);
14937 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14938 name = build_qualified_name (/*type=*/NULL_TREE,
14939 base, name,
14940 /*template_p=*/false);
14941 }
14942 else if (BASELINK_P (name))
14943 name = tsubst_baselink (name,
14944 non_reference (TREE_TYPE (object)),
14945 args, complain,
14946 in_decl);
14947 else
14948 name = tsubst_copy (name, args, complain, in_decl);
14949 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14950 }
14951
14952 case PLUS_EXPR:
14953 case MINUS_EXPR:
14954 case MULT_EXPR:
14955 case TRUNC_DIV_EXPR:
14956 case CEIL_DIV_EXPR:
14957 case FLOOR_DIV_EXPR:
14958 case ROUND_DIV_EXPR:
14959 case EXACT_DIV_EXPR:
14960 case BIT_AND_EXPR:
14961 case BIT_IOR_EXPR:
14962 case BIT_XOR_EXPR:
14963 case TRUNC_MOD_EXPR:
14964 case FLOOR_MOD_EXPR:
14965 case TRUTH_ANDIF_EXPR:
14966 case TRUTH_ORIF_EXPR:
14967 case TRUTH_AND_EXPR:
14968 case TRUTH_OR_EXPR:
14969 case RSHIFT_EXPR:
14970 case LSHIFT_EXPR:
14971 case RROTATE_EXPR:
14972 case LROTATE_EXPR:
14973 case EQ_EXPR:
14974 case NE_EXPR:
14975 case MAX_EXPR:
14976 case MIN_EXPR:
14977 case LE_EXPR:
14978 case GE_EXPR:
14979 case LT_EXPR:
14980 case GT_EXPR:
14981 case COMPOUND_EXPR:
14982 case DOTSTAR_EXPR:
14983 case MEMBER_REF:
14984 case PREDECREMENT_EXPR:
14985 case PREINCREMENT_EXPR:
14986 case POSTDECREMENT_EXPR:
14987 case POSTINCREMENT_EXPR:
14988 {
14989 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14990 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14991 return build_nt (code, op0, op1);
14992 }
14993
14994 case SCOPE_REF:
14995 {
14996 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14997 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14998 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14999 QUALIFIED_NAME_IS_TEMPLATE (t));
15000 }
15001
15002 case ARRAY_REF:
15003 {
15004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15005 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15006 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15007 }
15008
15009 case CALL_EXPR:
15010 {
15011 int n = VL_EXP_OPERAND_LENGTH (t);
15012 tree result = build_vl_exp (CALL_EXPR, n);
15013 int i;
15014 for (i = 0; i < n; i++)
15015 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15016 complain, in_decl);
15017 return result;
15018 }
15019
15020 case COND_EXPR:
15021 case MODOP_EXPR:
15022 case PSEUDO_DTOR_EXPR:
15023 case VEC_PERM_EXPR:
15024 {
15025 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15026 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15027 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15028 r = build_nt (code, op0, op1, op2);
15029 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15030 return r;
15031 }
15032
15033 case NEW_EXPR:
15034 {
15035 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15036 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15037 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15038 r = build_nt (code, op0, op1, op2);
15039 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15040 return r;
15041 }
15042
15043 case DELETE_EXPR:
15044 {
15045 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15046 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15047 r = build_nt (code, op0, op1);
15048 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15049 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15050 return r;
15051 }
15052
15053 case TEMPLATE_ID_EXPR:
15054 {
15055 /* Substituted template arguments */
15056 tree fn = TREE_OPERAND (t, 0);
15057 tree targs = TREE_OPERAND (t, 1);
15058
15059 fn = tsubst_copy (fn, args, complain, in_decl);
15060 if (targs)
15061 targs = tsubst_template_args (targs, args, complain, in_decl);
15062
15063 return lookup_template_function (fn, targs);
15064 }
15065
15066 case TREE_LIST:
15067 {
15068 tree purpose, value, chain;
15069
15070 if (t == void_list_node)
15071 return t;
15072
15073 purpose = TREE_PURPOSE (t);
15074 if (purpose)
15075 purpose = tsubst_copy (purpose, args, complain, in_decl);
15076 value = TREE_VALUE (t);
15077 if (value)
15078 value = tsubst_copy (value, args, complain, in_decl);
15079 chain = TREE_CHAIN (t);
15080 if (chain && chain != void_type_node)
15081 chain = tsubst_copy (chain, args, complain, in_decl);
15082 if (purpose == TREE_PURPOSE (t)
15083 && value == TREE_VALUE (t)
15084 && chain == TREE_CHAIN (t))
15085 return t;
15086 return tree_cons (purpose, value, chain);
15087 }
15088
15089 case RECORD_TYPE:
15090 case UNION_TYPE:
15091 case ENUMERAL_TYPE:
15092 case INTEGER_TYPE:
15093 case TEMPLATE_TYPE_PARM:
15094 case TEMPLATE_TEMPLATE_PARM:
15095 case BOUND_TEMPLATE_TEMPLATE_PARM:
15096 case TEMPLATE_PARM_INDEX:
15097 case POINTER_TYPE:
15098 case REFERENCE_TYPE:
15099 case OFFSET_TYPE:
15100 case FUNCTION_TYPE:
15101 case METHOD_TYPE:
15102 case ARRAY_TYPE:
15103 case TYPENAME_TYPE:
15104 case UNBOUND_CLASS_TEMPLATE:
15105 case TYPEOF_TYPE:
15106 case DECLTYPE_TYPE:
15107 case TYPE_DECL:
15108 return tsubst (t, args, complain, in_decl);
15109
15110 case USING_DECL:
15111 t = DECL_NAME (t);
15112 /* Fall through. */
15113 case IDENTIFIER_NODE:
15114 if (IDENTIFIER_TYPENAME_P (t))
15115 {
15116 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15117 return mangle_conv_op_name_for_type (new_type);
15118 }
15119 else
15120 return t;
15121
15122 case CONSTRUCTOR:
15123 /* This is handled by tsubst_copy_and_build. */
15124 gcc_unreachable ();
15125
15126 case VA_ARG_EXPR:
15127 {
15128 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15129 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15130 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15131 }
15132
15133 case CLEANUP_POINT_EXPR:
15134 /* We shouldn't have built any of these during initial template
15135 generation. Instead, they should be built during instantiation
15136 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15137 gcc_unreachable ();
15138
15139 case OFFSET_REF:
15140 {
15141 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15142 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15143 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15144 r = build2 (code, type, op0, op1);
15145 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15146 if (!mark_used (TREE_OPERAND (r, 1), complain)
15147 && !(complain & tf_error))
15148 return error_mark_node;
15149 return r;
15150 }
15151
15152 case EXPR_PACK_EXPANSION:
15153 error ("invalid use of pack expansion expression");
15154 return error_mark_node;
15155
15156 case NONTYPE_ARGUMENT_PACK:
15157 error ("use %<...%> to expand argument pack");
15158 return error_mark_node;
15159
15160 case VOID_CST:
15161 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15162 return t;
15163
15164 case INTEGER_CST:
15165 case REAL_CST:
15166 case STRING_CST:
15167 case COMPLEX_CST:
15168 {
15169 /* Instantiate any typedefs in the type. */
15170 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15171 r = fold_convert (type, t);
15172 gcc_assert (TREE_CODE (r) == code);
15173 return r;
15174 }
15175
15176 case PTRMEM_CST:
15177 /* These can sometimes show up in a partial instantiation, but never
15178 involve template parms. */
15179 gcc_assert (!uses_template_parms (t));
15180 return t;
15181
15182 case UNARY_LEFT_FOLD_EXPR:
15183 return tsubst_unary_left_fold (t, args, complain, in_decl);
15184 case UNARY_RIGHT_FOLD_EXPR:
15185 return tsubst_unary_right_fold (t, args, complain, in_decl);
15186 case BINARY_LEFT_FOLD_EXPR:
15187 return tsubst_binary_left_fold (t, args, complain, in_decl);
15188 case BINARY_RIGHT_FOLD_EXPR:
15189 return tsubst_binary_right_fold (t, args, complain, in_decl);
15190
15191 default:
15192 /* We shouldn't get here, but keep going if !flag_checking. */
15193 if (flag_checking)
15194 gcc_unreachable ();
15195 return t;
15196 }
15197}
15198
15199/* Helper function for tsubst_omp_clauses, used for instantiation of
15200 OMP_CLAUSE_DECL of clauses. */
15201
15202static tree
15203tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15204 tree in_decl)
15205{
15206 if (decl == NULL_TREE)
15207 return NULL_TREE;
15208
15209 /* Handle an OpenMP array section represented as a TREE_LIST (or
15210 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15211 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15212 TREE_LIST. We can handle it exactly the same as an array section
15213 (purpose, value, and a chain), even though the nomenclature
15214 (low_bound, length, etc) is different. */
15215 if (TREE_CODE (decl) == TREE_LIST)
15216 {
15217 tree low_bound
15218 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15219 /*integral_constant_expression_p=*/false);
15220 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15221 /*integral_constant_expression_p=*/false);
15222 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15223 in_decl);
15224 if (TREE_PURPOSE (decl) == low_bound
15225 && TREE_VALUE (decl) == length
15226 && TREE_CHAIN (decl) == chain)
15227 return decl;
15228 tree ret = tree_cons (low_bound, length, chain);
15229 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15230 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15231 return ret;
15232 }
15233 tree ret = tsubst_expr (decl, args, complain, in_decl,
15234 /*integral_constant_expression_p=*/false);
15235 /* Undo convert_from_reference tsubst_expr could have called. */
15236 if (decl
15237 && REFERENCE_REF_P (ret)
15238 && !REFERENCE_REF_P (decl))
15239 ret = TREE_OPERAND (ret, 0);
15240 return ret;
15241}
15242
15243/* Like tsubst_copy, but specifically for OpenMP clauses. */
15244
15245static tree
15246tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15247 tree args, tsubst_flags_t complain, tree in_decl)
15248{
15249 tree new_clauses = NULL_TREE, nc, oc;
15250 tree linear_no_step = NULL_TREE;
15251
15252 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15253 {
15254 nc = copy_node (oc);
15255 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15256 new_clauses = nc;
15257
15258 switch (OMP_CLAUSE_CODE (nc))
15259 {
15260 case OMP_CLAUSE_LASTPRIVATE:
15261 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15262 {
15263 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15264 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15265 in_decl, /*integral_constant_expression_p=*/false);
15266 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15267 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15268 }
15269 /* FALLTHRU */
15270 case OMP_CLAUSE_PRIVATE:
15271 case OMP_CLAUSE_SHARED:
15272 case OMP_CLAUSE_FIRSTPRIVATE:
15273 case OMP_CLAUSE_COPYIN:
15274 case OMP_CLAUSE_COPYPRIVATE:
15275 case OMP_CLAUSE_UNIFORM:
15276 case OMP_CLAUSE_DEPEND:
15277 case OMP_CLAUSE_FROM:
15278 case OMP_CLAUSE_TO:
15279 case OMP_CLAUSE_MAP:
15280 case OMP_CLAUSE_USE_DEVICE_PTR:
15281 case OMP_CLAUSE_IS_DEVICE_PTR:
15282 OMP_CLAUSE_DECL (nc)
15283 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15284 in_decl);
15285 break;
15286 case OMP_CLAUSE_TILE:
15287 case OMP_CLAUSE_IF:
15288 case OMP_CLAUSE_NUM_THREADS:
15289 case OMP_CLAUSE_SCHEDULE:
15290 case OMP_CLAUSE_COLLAPSE:
15291 case OMP_CLAUSE_FINAL:
15292 case OMP_CLAUSE_DEVICE:
15293 case OMP_CLAUSE_DIST_SCHEDULE:
15294 case OMP_CLAUSE_NUM_TEAMS:
15295 case OMP_CLAUSE_THREAD_LIMIT:
15296 case OMP_CLAUSE_SAFELEN:
15297 case OMP_CLAUSE_SIMDLEN:
15298 case OMP_CLAUSE_NUM_TASKS:
15299 case OMP_CLAUSE_GRAINSIZE:
15300 case OMP_CLAUSE_PRIORITY:
15301 case OMP_CLAUSE_ORDERED:
15302 case OMP_CLAUSE_HINT:
15303 case OMP_CLAUSE_NUM_GANGS:
15304 case OMP_CLAUSE_NUM_WORKERS:
15305 case OMP_CLAUSE_VECTOR_LENGTH:
15306 case OMP_CLAUSE_WORKER:
15307 case OMP_CLAUSE_VECTOR:
15308 case OMP_CLAUSE_ASYNC:
15309 case OMP_CLAUSE_WAIT:
15310 OMP_CLAUSE_OPERAND (nc, 0)
15311 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15312 in_decl, /*integral_constant_expression_p=*/false);
15313 break;
15314 case OMP_CLAUSE_REDUCTION:
15315 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15316 {
15317 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15318 if (TREE_CODE (placeholder) == SCOPE_REF)
15319 {
15320 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15321 complain, in_decl);
15322 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15323 = build_qualified_name (NULL_TREE, scope,
15324 TREE_OPERAND (placeholder, 1),
15325 false);
15326 }
15327 else
15328 gcc_assert (identifier_p (placeholder));
15329 }
15330 OMP_CLAUSE_DECL (nc)
15331 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15332 in_decl);
15333 break;
15334 case OMP_CLAUSE_GANG:
15335 case OMP_CLAUSE_ALIGNED:
15336 OMP_CLAUSE_DECL (nc)
15337 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15338 in_decl);
15339 OMP_CLAUSE_OPERAND (nc, 1)
15340 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15341 in_decl, /*integral_constant_expression_p=*/false);
15342 break;
15343 case OMP_CLAUSE_LINEAR:
15344 OMP_CLAUSE_DECL (nc)
15345 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15346 in_decl);
15347 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15348 {
15349 gcc_assert (!linear_no_step);
15350 linear_no_step = nc;
15351 }
15352 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15353 OMP_CLAUSE_LINEAR_STEP (nc)
15354 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15355 complain, in_decl);
15356 else
15357 OMP_CLAUSE_LINEAR_STEP (nc)
15358 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15359 in_decl,
15360 /*integral_constant_expression_p=*/false);
15361 break;
15362 case OMP_CLAUSE_NOWAIT:
15363 case OMP_CLAUSE_DEFAULT:
15364 case OMP_CLAUSE_UNTIED:
15365 case OMP_CLAUSE_MERGEABLE:
15366 case OMP_CLAUSE_INBRANCH:
15367 case OMP_CLAUSE_NOTINBRANCH:
15368 case OMP_CLAUSE_PROC_BIND:
15369 case OMP_CLAUSE_FOR:
15370 case OMP_CLAUSE_PARALLEL:
15371 case OMP_CLAUSE_SECTIONS:
15372 case OMP_CLAUSE_TASKGROUP:
15373 case OMP_CLAUSE_NOGROUP:
15374 case OMP_CLAUSE_THREADS:
15375 case OMP_CLAUSE_SIMD:
15376 case OMP_CLAUSE_DEFAULTMAP:
15377 case OMP_CLAUSE_INDEPENDENT:
15378 case OMP_CLAUSE_AUTO:
15379 case OMP_CLAUSE_SEQ:
15380 break;
15381 default:
15382 gcc_unreachable ();
15383 }
15384 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15385 switch (OMP_CLAUSE_CODE (nc))
15386 {
15387 case OMP_CLAUSE_SHARED:
15388 case OMP_CLAUSE_PRIVATE:
15389 case OMP_CLAUSE_FIRSTPRIVATE:
15390 case OMP_CLAUSE_LASTPRIVATE:
15391 case OMP_CLAUSE_COPYPRIVATE:
15392 case OMP_CLAUSE_LINEAR:
15393 case OMP_CLAUSE_REDUCTION:
15394 case OMP_CLAUSE_USE_DEVICE_PTR:
15395 case OMP_CLAUSE_IS_DEVICE_PTR:
15396 /* tsubst_expr on SCOPE_REF results in returning
15397 finish_non_static_data_member result. Undo that here. */
15398 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15399 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15400 == IDENTIFIER_NODE))
15401 {
15402 tree t = OMP_CLAUSE_DECL (nc);
15403 tree v = t;
15404 while (v)
15405 switch (TREE_CODE (v))
15406 {
15407 case COMPONENT_REF:
15408 case MEM_REF:
15409 case INDIRECT_REF:
15410 CASE_CONVERT:
15411 case POINTER_PLUS_EXPR:
15412 v = TREE_OPERAND (v, 0);
15413 continue;
15414 case PARM_DECL:
15415 if (DECL_CONTEXT (v) == current_function_decl
15416 && DECL_ARTIFICIAL (v)
15417 && DECL_NAME (v) == this_identifier)
15418 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15419 /* FALLTHRU */
15420 default:
15421 v = NULL_TREE;
15422 break;
15423 }
15424 }
15425 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15426 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15427 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15428 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15429 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15430 {
15431 tree decl = OMP_CLAUSE_DECL (nc);
15432 if (VAR_P (decl))
15433 {
15434 if (!DECL_LANG_SPECIFIC (decl))
15435 retrofit_lang_decl (decl);
15436 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15437 }
15438 }
15439 break;
15440 default:
15441 break;
15442 }
15443 }
15444
15445 new_clauses = nreverse (new_clauses);
15446 if (ort != C_ORT_OMP_DECLARE_SIMD)
15447 {
15448 new_clauses = finish_omp_clauses (new_clauses, ort);
15449 if (linear_no_step)
15450 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15451 if (nc == linear_no_step)
15452 {
15453 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15454 break;
15455 }
15456 }
15457 return new_clauses;
15458}
15459
15460/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15461
15462static tree
15463tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15464 tree in_decl)
15465{
15466#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15467
15468 tree purpose, value, chain;
15469
15470 if (t == NULL)
15471 return t;
15472
15473 if (TREE_CODE (t) != TREE_LIST)
15474 return tsubst_copy_and_build (t, args, complain, in_decl,
15475 /*function_p=*/false,
15476 /*integral_constant_expression_p=*/false);
15477
15478 if (t == void_list_node)
15479 return t;
15480
15481 purpose = TREE_PURPOSE (t);
15482 if (purpose)
15483 purpose = RECUR (purpose);
15484 value = TREE_VALUE (t);
15485 if (value)
15486 {
15487 if (TREE_CODE (value) != LABEL_DECL)
15488 value = RECUR (value);
15489 else
15490 {
15491 value = lookup_label (DECL_NAME (value));
15492 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15493 TREE_USED (value) = 1;
15494 }
15495 }
15496 chain = TREE_CHAIN (t);
15497 if (chain && chain != void_type_node)
15498 chain = RECUR (chain);
15499 return tree_cons (purpose, value, chain);
15500#undef RECUR
15501}
15502
15503/* Used to temporarily communicate the list of #pragma omp parallel
15504 clauses to #pragma omp for instantiation if they are combined
15505 together. */
15506
15507static tree *omp_parallel_combined_clauses;
15508
15509/* Substitute one OMP_FOR iterator. */
15510
15511static void
15512tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15513 tree initv, tree condv, tree incrv, tree *clauses,
15514 tree args, tsubst_flags_t complain, tree in_decl,
15515 bool integral_constant_expression_p)
15516{
15517#define RECUR(NODE) \
15518 tsubst_expr ((NODE), args, complain, in_decl, \
15519 integral_constant_expression_p)
15520 tree decl, init, cond, incr;
15521
15522 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15523 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15524
15525 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15526 {
15527 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15528 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15529 }
15530
15531 decl = TREE_OPERAND (init, 0);
15532 init = TREE_OPERAND (init, 1);
15533 tree decl_expr = NULL_TREE;
15534 if (init && TREE_CODE (init) == DECL_EXPR)
15535 {
15536 /* We need to jump through some hoops to handle declarations in the
15537 init-statement, since we might need to handle auto deduction,
15538 but we need to keep control of initialization. */
15539 decl_expr = init;
15540 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15541 decl = tsubst_decl (decl, args, complain);
15542 }
15543 else
15544 {
15545 if (TREE_CODE (decl) == SCOPE_REF)
15546 {
15547 decl = RECUR (decl);
15548 if (TREE_CODE (decl) == COMPONENT_REF)
15549 {
15550 tree v = decl;
15551 while (v)
15552 switch (TREE_CODE (v))
15553 {
15554 case COMPONENT_REF:
15555 case MEM_REF:
15556 case INDIRECT_REF:
15557 CASE_CONVERT:
15558 case POINTER_PLUS_EXPR:
15559 v = TREE_OPERAND (v, 0);
15560 continue;
15561 case PARM_DECL:
15562 if (DECL_CONTEXT (v) == current_function_decl
15563 && DECL_ARTIFICIAL (v)
15564 && DECL_NAME (v) == this_identifier)
15565 {
15566 decl = TREE_OPERAND (decl, 1);
15567 decl = omp_privatize_field (decl, false);
15568 }
15569 /* FALLTHRU */
15570 default:
15571 v = NULL_TREE;
15572 break;
15573 }
15574 }
15575 }
15576 else
15577 decl = RECUR (decl);
15578 }
15579 init = RECUR (init);
15580
15581 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15582 if (auto_node && init)
15583 TREE_TYPE (decl)
15584 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15585
15586 gcc_assert (!type_dependent_expression_p (decl));
15587
15588 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15589 {
15590 if (decl_expr)
15591 {
15592 /* Declare the variable, but don't let that initialize it. */
15593 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15594 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15595 RECUR (decl_expr);
15596 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15597 }
15598
15599 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15600 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15601 if (TREE_CODE (incr) == MODIFY_EXPR)
15602 {
15603 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15604 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15605 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15606 NOP_EXPR, rhs, complain);
15607 }
15608 else
15609 incr = RECUR (incr);
15610 TREE_VEC_ELT (declv, i) = decl;
15611 TREE_VEC_ELT (initv, i) = init;
15612 TREE_VEC_ELT (condv, i) = cond;
15613 TREE_VEC_ELT (incrv, i) = incr;
15614 return;
15615 }
15616
15617 if (decl_expr)
15618 {
15619 /* Declare and initialize the variable. */
15620 RECUR (decl_expr);
15621 init = NULL_TREE;
15622 }
15623 else if (init)
15624 {
15625 tree *pc;
15626 int j;
15627 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15628 {
15629 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15630 {
15631 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15632 && OMP_CLAUSE_DECL (*pc) == decl)
15633 break;
15634 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15635 && OMP_CLAUSE_DECL (*pc) == decl)
15636 {
15637 if (j)
15638 break;
15639 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15640 tree c = *pc;
15641 *pc = OMP_CLAUSE_CHAIN (c);
15642 OMP_CLAUSE_CHAIN (c) = *clauses;
15643 *clauses = c;
15644 }
15645 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15646 && OMP_CLAUSE_DECL (*pc) == decl)
15647 {
15648 error ("iteration variable %qD should not be firstprivate",
15649 decl);
15650 *pc = OMP_CLAUSE_CHAIN (*pc);
15651 }
15652 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15653 && OMP_CLAUSE_DECL (*pc) == decl)
15654 {
15655 error ("iteration variable %qD should not be reduction",
15656 decl);
15657 *pc = OMP_CLAUSE_CHAIN (*pc);
15658 }
15659 else
15660 pc = &OMP_CLAUSE_CHAIN (*pc);
15661 }
15662 if (*pc)
15663 break;
15664 }
15665 if (*pc == NULL_TREE)
15666 {
15667 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15668 OMP_CLAUSE_DECL (c) = decl;
15669 c = finish_omp_clauses (c, C_ORT_OMP);
15670 if (c)
15671 {
15672 OMP_CLAUSE_CHAIN (c) = *clauses;
15673 *clauses = c;
15674 }
15675 }
15676 }
15677 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15678 if (COMPARISON_CLASS_P (cond))
15679 {
15680 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15681 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15682 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15683 }
15684 else
15685 cond = RECUR (cond);
15686 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15687 switch (TREE_CODE (incr))
15688 {
15689 case PREINCREMENT_EXPR:
15690 case PREDECREMENT_EXPR:
15691 case POSTINCREMENT_EXPR:
15692 case POSTDECREMENT_EXPR:
15693 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15694 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15695 break;
15696 case MODIFY_EXPR:
15697 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15698 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15699 {
15700 tree rhs = TREE_OPERAND (incr, 1);
15701 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15702 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15703 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15704 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15705 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15706 rhs0, rhs1));
15707 }
15708 else
15709 incr = RECUR (incr);
15710 break;
15711 case MODOP_EXPR:
15712 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15713 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15714 {
15715 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15716 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15717 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15718 TREE_TYPE (decl), lhs,
15719 RECUR (TREE_OPERAND (incr, 2))));
15720 }
15721 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15722 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15723 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15724 {
15725 tree rhs = TREE_OPERAND (incr, 2);
15726 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15727 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15728 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15729 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15730 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15731 rhs0, rhs1));
15732 }
15733 else
15734 incr = RECUR (incr);
15735 break;
15736 default:
15737 incr = RECUR (incr);
15738 break;
15739 }
15740
15741 TREE_VEC_ELT (declv, i) = decl;
15742 TREE_VEC_ELT (initv, i) = init;
15743 TREE_VEC_ELT (condv, i) = cond;
15744 TREE_VEC_ELT (incrv, i) = incr;
15745#undef RECUR
15746}
15747
15748/* Helper function of tsubst_expr, find OMP_TEAMS inside
15749 of OMP_TARGET's body. */
15750
15751static tree
15752tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15753{
15754 *walk_subtrees = 0;
15755 switch (TREE_CODE (*tp))
15756 {
15757 case OMP_TEAMS:
15758 return *tp;
15759 case BIND_EXPR:
15760 case STATEMENT_LIST:
15761 *walk_subtrees = 1;
15762 break;
15763 default:
15764 break;
15765 }
15766 return NULL_TREE;
15767}
15768
15769/* Helper function for tsubst_expr. For decomposition declaration
15770 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15771 also the corresponding decls representing the identifiers
15772 of the decomposition declaration. Return DECL if successful
15773 or error_mark_node otherwise, set *FIRST to the first decl
15774 in the list chained through DECL_CHAIN and *CNT to the number
15775 of such decls. */
15776
15777static tree
15778tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15779 tsubst_flags_t complain, tree in_decl, tree *first,
15780 unsigned int *cnt)
15781{
15782 tree decl2, decl3, prev = decl;
15783 *cnt = 0;
15784 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15785 for (decl2 = DECL_CHAIN (pattern_decl);
15786 decl2
15787 && VAR_P (decl2)
15788 && DECL_DECOMPOSITION_P (decl2)
15789 && DECL_NAME (decl2);
15790 decl2 = DECL_CHAIN (decl2))
15791 {
15792 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15793 {
15794 gcc_assert (errorcount);
15795 return error_mark_node;
15796 }
15797 (*cnt)++;
15798 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15799 tree v = DECL_VALUE_EXPR (decl2);
15800 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15801 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15802 decl3 = tsubst (decl2, args, complain, in_decl);
15803 SET_DECL_VALUE_EXPR (decl2, v);
15804 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15805 if (VAR_P (decl3))
15806 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15807 else
15808 {
15809 gcc_assert (errorcount);
15810 decl = error_mark_node;
15811 continue;
15812 }
15813 maybe_push_decl (decl3);
15814 if (error_operand_p (decl3))
15815 decl = error_mark_node;
15816 else if (decl != error_mark_node
15817 && DECL_CHAIN (decl3) != prev)
15818 {
15819 gcc_assert (errorcount);
15820 decl = error_mark_node;
15821 }
15822 else
15823 prev = decl3;
15824 }
15825 *first = prev;
15826 return decl;
15827}
15828
15829/* Like tsubst_copy for expressions, etc. but also does semantic
15830 processing. */
15831
15832tree
15833tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15834 bool integral_constant_expression_p)
15835{
15836#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15837#define RECUR(NODE) \
15838 tsubst_expr ((NODE), args, complain, in_decl, \
15839 integral_constant_expression_p)
15840
15841 tree stmt, tmp;
15842 tree r;
15843 location_t loc;
15844
15845 if (t == NULL_TREE || t == error_mark_node)
15846 return t;
15847
15848 loc = input_location;
15849 if (EXPR_HAS_LOCATION (t))
15850 input_location = EXPR_LOCATION (t);
15851 if (STATEMENT_CODE_P (TREE_CODE (t)))
15852 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15853
15854 switch (TREE_CODE (t))
15855 {
15856 case STATEMENT_LIST:
15857 {
15858 tree_stmt_iterator i;
15859 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15860 RECUR (tsi_stmt (i));
15861 break;
15862 }
15863
15864 case CTOR_INITIALIZER:
15865 finish_mem_initializers (tsubst_initializer_list
15866 (TREE_OPERAND (t, 0), args));
15867 break;
15868
15869 case RETURN_EXPR:
15870 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15871 break;
15872
15873 case EXPR_STMT:
15874 tmp = RECUR (EXPR_STMT_EXPR (t));
15875 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15876 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15877 else
15878 finish_expr_stmt (tmp);
15879 break;
15880
15881 case USING_STMT:
15882 do_using_directive (USING_STMT_NAMESPACE (t));
15883 break;
15884
15885 case DECL_EXPR:
15886 {
15887 tree decl, pattern_decl;
15888 tree init;
15889
15890 pattern_decl = decl = DECL_EXPR_DECL (t);
15891 if (TREE_CODE (decl) == LABEL_DECL)
15892 finish_label_decl (DECL_NAME (decl));
15893 else if (TREE_CODE (decl) == USING_DECL)
15894 {
15895 tree scope = USING_DECL_SCOPE (decl);
15896 tree name = DECL_NAME (decl);
15897
15898 scope = tsubst (scope, args, complain, in_decl);
15899 decl = lookup_qualified_name (scope, name,
15900 /*is_type_p=*/false,
15901 /*complain=*/false);
15902 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15903 qualified_name_lookup_error (scope, name, decl, input_location);
15904 else
15905 do_local_using_decl (decl, scope, name);
15906 }
15907 else if (DECL_PACK_P (decl))
15908 {
15909 /* Don't build up decls for a variadic capture proxy, we'll
15910 instantiate the elements directly as needed. */
15911 break;
15912 }
15913 else
15914 {
15915 init = DECL_INITIAL (decl);
15916 decl = tsubst (decl, args, complain, in_decl);
15917 if (decl != error_mark_node)
15918 {
15919 /* By marking the declaration as instantiated, we avoid
15920 trying to instantiate it. Since instantiate_decl can't
15921 handle local variables, and since we've already done
15922 all that needs to be done, that's the right thing to
15923 do. */
15924 if (VAR_P (decl))
15925 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15926 if (VAR_P (decl)
15927 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15928 /* Anonymous aggregates are a special case. */
15929 finish_anon_union (decl);
15930 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15931 {
15932 DECL_CONTEXT (decl) = current_function_decl;
15933 if (DECL_NAME (decl) == this_identifier)
15934 {
15935 tree lam = DECL_CONTEXT (current_function_decl);
15936 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15937 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15938 }
15939 insert_capture_proxy (decl);
15940 }
15941 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15942 /* We already did a pushtag. */;
15943 else if (TREE_CODE (decl) == FUNCTION_DECL
15944 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15945 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15946 {
15947 DECL_CONTEXT (decl) = NULL_TREE;
15948 pushdecl (decl);
15949 DECL_CONTEXT (decl) = current_function_decl;
15950 cp_check_omp_declare_reduction (decl);
15951 }
15952 else
15953 {
15954 int const_init = false;
15955 maybe_push_decl (decl);
15956 if (VAR_P (decl)
15957 && DECL_PRETTY_FUNCTION_P (decl))
15958 {
15959 /* For __PRETTY_FUNCTION__ we have to adjust the
15960 initializer. */
15961 const char *const name
15962 = cxx_printable_name (current_function_decl, 2);
15963 init = cp_fname_init (name, &TREE_TYPE (decl));
15964 }
15965 else
15966 init = tsubst_init (init, decl, args, complain, in_decl);
15967
15968 if (VAR_P (decl))
15969 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15970 (pattern_decl));
15971 if (VAR_P (decl)
15972 && DECL_DECOMPOSITION_P (decl)
15973 && TREE_TYPE (pattern_decl) != error_mark_node)
15974 {
15975 unsigned int cnt;
15976 tree first;
15977 tree ndecl
15978 = tsubst_decomp_names (decl, pattern_decl, args,
15979 complain, in_decl, &first, &cnt);
15980 if (ndecl != error_mark_node)
15981 cp_maybe_mangle_decomp (ndecl, first, cnt);
15982 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15983 if (ndecl != error_mark_node)
15984 cp_finish_decomp (ndecl, first, cnt);
15985 }
15986 else
15987 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15988 }
15989 }
15990 }
15991
15992 break;
15993 }
15994
15995 case FOR_STMT:
15996 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15997 RECUR (FOR_INIT_STMT (t));
15998 finish_init_stmt (stmt);
15999 tmp = RECUR (FOR_COND (t));
16000 finish_for_cond (tmp, stmt, false);
16001 tmp = RECUR (FOR_EXPR (t));
16002 finish_for_expr (tmp, stmt);
16003 RECUR (FOR_BODY (t));
16004 finish_for_stmt (stmt);
16005 break;
16006
16007 case RANGE_FOR_STMT:
16008 {
16009 tree decl, expr;
16010 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16011 decl = RANGE_FOR_DECL (t);
16012 decl = tsubst (decl, args, complain, in_decl);
16013 maybe_push_decl (decl);
16014 expr = RECUR (RANGE_FOR_EXPR (t));
16015 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16016 {
16017 unsigned int cnt;
16018 tree first;
16019 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16020 complain, in_decl, &first, &cnt);
16021 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16022 RANGE_FOR_IVDEP (t));
16023 }
16024 else
16025 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16026 RANGE_FOR_IVDEP (t));
16027 RECUR (RANGE_FOR_BODY (t));
16028 finish_for_stmt (stmt);
16029 }
16030 break;
16031
16032 case WHILE_STMT:
16033 stmt = begin_while_stmt ();
16034 tmp = RECUR (WHILE_COND (t));
16035 finish_while_stmt_cond (tmp, stmt, false);
16036 RECUR (WHILE_BODY (t));
16037 finish_while_stmt (stmt);
16038 break;
16039
16040 case DO_STMT:
16041 stmt = begin_do_stmt ();
16042 RECUR (DO_BODY (t));
16043 finish_do_body (stmt);
16044 tmp = RECUR (DO_COND (t));
16045 finish_do_stmt (tmp, stmt, false);
16046 break;
16047
16048 case IF_STMT:
16049 stmt = begin_if_stmt ();
16050 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16051 tmp = RECUR (IF_COND (t));
16052 tmp = finish_if_stmt_cond (tmp, stmt);
16053 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16054 /* Don't instantiate the THEN_CLAUSE. */;
16055 else
16056 {
16057 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16058 if (inhibit)
16059 ++c_inhibit_evaluation_warnings;
16060 RECUR (THEN_CLAUSE (t));
16061 if (inhibit)
16062 --c_inhibit_evaluation_warnings;
16063 }
16064 finish_then_clause (stmt);
16065
16066 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16067 /* Don't instantiate the ELSE_CLAUSE. */;
16068 else if (ELSE_CLAUSE (t))
16069 {
16070 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16071 begin_else_clause (stmt);
16072 if (inhibit)
16073 ++c_inhibit_evaluation_warnings;
16074 RECUR (ELSE_CLAUSE (t));
16075 if (inhibit)
16076 --c_inhibit_evaluation_warnings;
16077 finish_else_clause (stmt);
16078 }
16079
16080 finish_if_stmt (stmt);
16081 break;
16082
16083 case BIND_EXPR:
16084 if (BIND_EXPR_BODY_BLOCK (t))
16085 stmt = begin_function_body ();
16086 else
16087 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16088 ? BCS_TRY_BLOCK : 0);
16089
16090 RECUR (BIND_EXPR_BODY (t));
16091
16092 if (BIND_EXPR_BODY_BLOCK (t))
16093 finish_function_body (stmt);
16094 else
16095 finish_compound_stmt (stmt);
16096 break;
16097
16098 case BREAK_STMT:
16099 finish_break_stmt ();
16100 break;
16101
16102 case CONTINUE_STMT:
16103 finish_continue_stmt ();
16104 break;
16105
16106 case SWITCH_STMT:
16107 stmt = begin_switch_stmt ();
16108 tmp = RECUR (SWITCH_STMT_COND (t));
16109 finish_switch_cond (tmp, stmt);
16110 RECUR (SWITCH_STMT_BODY (t));
16111 finish_switch_stmt (stmt);
16112 break;
16113
16114 case CASE_LABEL_EXPR:
16115 {
16116 tree low = RECUR (CASE_LOW (t));
16117 tree high = RECUR (CASE_HIGH (t));
16118 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16119 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16120 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16121 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16122 }
16123 break;
16124
16125 case LABEL_EXPR:
16126 {
16127 tree decl = LABEL_EXPR_LABEL (t);
16128 tree label;
16129
16130 label = finish_label_stmt (DECL_NAME (decl));
16131 if (TREE_CODE (label) == LABEL_DECL)
16132 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16133 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16134 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16135 }
16136 break;
16137
16138 case GOTO_EXPR:
16139 tmp = GOTO_DESTINATION (t);
16140 if (TREE_CODE (tmp) != LABEL_DECL)
16141 /* Computed goto's must be tsubst'd into. On the other hand,
16142 non-computed gotos must not be; the identifier in question
16143 will have no binding. */
16144 tmp = RECUR (tmp);
16145 else
16146 tmp = DECL_NAME (tmp);
16147 finish_goto_stmt (tmp);
16148 break;
16149
16150 case ASM_EXPR:
16151 {
16152 tree string = RECUR (ASM_STRING (t));
16153 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16154 complain, in_decl);
16155 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16156 complain, in_decl);
16157 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16158 complain, in_decl);
16159 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16160 complain, in_decl);
16161 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16162 clobbers, labels);
16163 tree asm_expr = tmp;
16164 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16165 asm_expr = TREE_OPERAND (asm_expr, 0);
16166 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16167 }
16168 break;
16169
16170 case TRY_BLOCK:
16171 if (CLEANUP_P (t))
16172 {
16173 stmt = begin_try_block ();
16174 RECUR (TRY_STMTS (t));
16175 finish_cleanup_try_block (stmt);
16176 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16177 }
16178 else
16179 {
16180 tree compound_stmt = NULL_TREE;
16181
16182 if (FN_TRY_BLOCK_P (t))
16183 stmt = begin_function_try_block (&compound_stmt);
16184 else
16185 stmt = begin_try_block ();
16186
16187 RECUR (TRY_STMTS (t));
16188
16189 if (FN_TRY_BLOCK_P (t))
16190 finish_function_try_block (stmt);
16191 else
16192 finish_try_block (stmt);
16193
16194 RECUR (TRY_HANDLERS (t));
16195 if (FN_TRY_BLOCK_P (t))
16196 finish_function_handler_sequence (stmt, compound_stmt);
16197 else
16198 finish_handler_sequence (stmt);
16199 }
16200 break;
16201
16202 case HANDLER:
16203 {
16204 tree decl = HANDLER_PARMS (t);
16205
16206 if (decl)
16207 {
16208 decl = tsubst (decl, args, complain, in_decl);
16209 /* Prevent instantiate_decl from trying to instantiate
16210 this variable. We've already done all that needs to be
16211 done. */
16212 if (decl != error_mark_node)
16213 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16214 }
16215 stmt = begin_handler ();
16216 finish_handler_parms (decl, stmt);
16217 RECUR (HANDLER_BODY (t));
16218 finish_handler (stmt);
16219 }
16220 break;
16221
16222 case TAG_DEFN:
16223 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16224 if (CLASS_TYPE_P (tmp))
16225 {
16226 /* Local classes are not independent templates; they are
16227 instantiated along with their containing function. And this
16228 way we don't have to deal with pushing out of one local class
16229 to instantiate a member of another local class. */
16230 tree fn;
16231 /* Closures are handled by the LAMBDA_EXPR. */
16232 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16233 complete_type (tmp);
16234 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16235 if (!DECL_ARTIFICIAL (fn))
16236 instantiate_decl (fn, /*defer_ok=*/false,
16237 /*expl_inst_class=*/false);
16238 }
16239 break;
16240
16241 case STATIC_ASSERT:
16242 {
16243 tree condition;
16244
16245 ++c_inhibit_evaluation_warnings;
16246 condition =
16247 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16248 args,
16249 complain, in_decl,
16250 /*integral_constant_expression_p=*/true);
16251 --c_inhibit_evaluation_warnings;
16252
16253 finish_static_assert (condition,
16254 STATIC_ASSERT_MESSAGE (t),
16255 STATIC_ASSERT_SOURCE_LOCATION (t),
16256 /*member_p=*/false);
16257 }
16258 break;
16259
16260 case OACC_KERNELS:
16261 case OACC_PARALLEL:
16262 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16263 in_decl);
16264 stmt = begin_omp_parallel ();
16265 RECUR (OMP_BODY (t));
16266 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16267 break;
16268
16269 case OMP_PARALLEL:
16270 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16271 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16272 complain, in_decl);
16273 if (OMP_PARALLEL_COMBINED (t))
16274 omp_parallel_combined_clauses = &tmp;
16275 stmt = begin_omp_parallel ();
16276 RECUR (OMP_PARALLEL_BODY (t));
16277 gcc_assert (omp_parallel_combined_clauses == NULL);
16278 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16279 = OMP_PARALLEL_COMBINED (t);
16280 pop_omp_privatization_clauses (r);
16281 break;
16282
16283 case OMP_TASK:
16284 r = push_omp_privatization_clauses (false);
16285 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16286 complain, in_decl);
16287 stmt = begin_omp_task ();
16288 RECUR (OMP_TASK_BODY (t));
16289 finish_omp_task (tmp, stmt);
16290 pop_omp_privatization_clauses (r);
16291 break;
16292
16293 case OMP_FOR:
16294 case OMP_SIMD:
16295 case CILK_SIMD:
16296 case CILK_FOR:
16297 case OMP_DISTRIBUTE:
16298 case OMP_TASKLOOP:
16299 case OACC_LOOP:
16300 {
16301 tree clauses, body, pre_body;
16302 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16303 tree orig_declv = NULL_TREE;
16304 tree incrv = NULL_TREE;
16305 enum c_omp_region_type ort = C_ORT_OMP;
16306 int i;
16307
16308 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16309 ort = C_ORT_CILK;
16310 else if (TREE_CODE (t) == OACC_LOOP)
16311 ort = C_ORT_ACC;
16312
16313 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16314 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16315 in_decl);
16316 if (OMP_FOR_INIT (t) != NULL_TREE)
16317 {
16318 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16319 if (OMP_FOR_ORIG_DECLS (t))
16320 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16321 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16322 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16323 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16324 }
16325
16326 stmt = begin_omp_structured_block ();
16327
16328 pre_body = push_stmt_list ();
16329 RECUR (OMP_FOR_PRE_BODY (t));
16330 pre_body = pop_stmt_list (pre_body);
16331
16332 if (OMP_FOR_INIT (t) != NULL_TREE)
16333 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16334 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16335 incrv, &clauses, args, complain, in_decl,
16336 integral_constant_expression_p);
16337 omp_parallel_combined_clauses = NULL;
16338
16339 body = push_stmt_list ();
16340 RECUR (OMP_FOR_BODY (t));
16341 body = pop_stmt_list (body);
16342
16343 if (OMP_FOR_INIT (t) != NULL_TREE)
16344 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16345 orig_declv, initv, condv, incrv, body, pre_body,
16346 NULL, clauses);
16347 else
16348 {
16349 t = make_node (TREE_CODE (t));
16350 TREE_TYPE (t) = void_type_node;
16351 OMP_FOR_BODY (t) = body;
16352 OMP_FOR_PRE_BODY (t) = pre_body;
16353 OMP_FOR_CLAUSES (t) = clauses;
16354 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16355 add_stmt (t);
16356 }
16357
16358 add_stmt (finish_omp_structured_block (stmt));
16359 pop_omp_privatization_clauses (r);
16360 }
16361 break;
16362
16363 case OMP_SECTIONS:
16364 omp_parallel_combined_clauses = NULL;
16365 /* FALLTHRU */
16366 case OMP_SINGLE:
16367 case OMP_TEAMS:
16368 case OMP_CRITICAL:
16369 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16370 && OMP_TEAMS_COMBINED (t));
16371 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16372 in_decl);
16373 stmt = push_stmt_list ();
16374 RECUR (OMP_BODY (t));
16375 stmt = pop_stmt_list (stmt);
16376
16377 t = copy_node (t);
16378 OMP_BODY (t) = stmt;
16379 OMP_CLAUSES (t) = tmp;
16380 add_stmt (t);
16381 pop_omp_privatization_clauses (r);
16382 break;
16383
16384 case OACC_DATA:
16385 case OMP_TARGET_DATA:
16386 case OMP_TARGET:
16387 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16388 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16389 in_decl);
16390 keep_next_level (true);
16391 stmt = begin_omp_structured_block ();
16392
16393 RECUR (OMP_BODY (t));
16394 stmt = finish_omp_structured_block (stmt);
16395
16396 t = copy_node (t);
16397 OMP_BODY (t) = stmt;
16398 OMP_CLAUSES (t) = tmp;
16399 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16400 {
16401 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16402 if (teams)
16403 {
16404 /* For combined target teams, ensure the num_teams and
16405 thread_limit clause expressions are evaluated on the host,
16406 before entering the target construct. */
16407 tree c;
16408 for (c = OMP_TEAMS_CLAUSES (teams);
16409 c; c = OMP_CLAUSE_CHAIN (c))
16410 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16411 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16412 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16413 {
16414 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16415 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16416 if (expr == error_mark_node)
16417 continue;
16418 tmp = TARGET_EXPR_SLOT (expr);
16419 add_stmt (expr);
16420 OMP_CLAUSE_OPERAND (c, 0) = expr;
16421 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16422 OMP_CLAUSE_FIRSTPRIVATE);
16423 OMP_CLAUSE_DECL (tc) = tmp;
16424 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16425 OMP_TARGET_CLAUSES (t) = tc;
16426 }
16427 }
16428 }
16429 add_stmt (t);
16430 break;
16431
16432 case OACC_DECLARE:
16433 t = copy_node (t);
16434 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16435 complain, in_decl);
16436 OACC_DECLARE_CLAUSES (t) = tmp;
16437 add_stmt (t);
16438 break;
16439
16440 case OMP_TARGET_UPDATE:
16441 case OMP_TARGET_ENTER_DATA:
16442 case OMP_TARGET_EXIT_DATA:
16443 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16444 complain, in_decl);
16445 t = copy_node (t);
16446 OMP_STANDALONE_CLAUSES (t) = tmp;
16447 add_stmt (t);
16448 break;
16449
16450 case OACC_ENTER_DATA:
16451 case OACC_EXIT_DATA:
16452 case OACC_UPDATE:
16453 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16454 complain, in_decl);
16455 t = copy_node (t);
16456 OMP_STANDALONE_CLAUSES (t) = tmp;
16457 add_stmt (t);
16458 break;
16459
16460 case OMP_ORDERED:
16461 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16462 complain, in_decl);
16463 stmt = push_stmt_list ();
16464 RECUR (OMP_BODY (t));
16465 stmt = pop_stmt_list (stmt);
16466
16467 t = copy_node (t);
16468 OMP_BODY (t) = stmt;
16469 OMP_ORDERED_CLAUSES (t) = tmp;
16470 add_stmt (t);
16471 break;
16472
16473 case OMP_SECTION:
16474 case OMP_MASTER:
16475 case OMP_TASKGROUP:
16476 stmt = push_stmt_list ();
16477 RECUR (OMP_BODY (t));
16478 stmt = pop_stmt_list (stmt);
16479
16480 t = copy_node (t);
16481 OMP_BODY (t) = stmt;
16482 add_stmt (t);
16483 break;
16484
16485 case OMP_ATOMIC:
16486 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16487 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16488 {
16489 tree op1 = TREE_OPERAND (t, 1);
16490 tree rhs1 = NULL_TREE;
16491 tree lhs, rhs;
16492 if (TREE_CODE (op1) == COMPOUND_EXPR)
16493 {
16494 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16495 op1 = TREE_OPERAND (op1, 1);
16496 }
16497 lhs = RECUR (TREE_OPERAND (op1, 0));
16498 rhs = RECUR (TREE_OPERAND (op1, 1));
16499 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16500 NULL_TREE, NULL_TREE, rhs1,
16501 OMP_ATOMIC_SEQ_CST (t));
16502 }
16503 else
16504 {
16505 tree op1 = TREE_OPERAND (t, 1);
16506 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16507 tree rhs1 = NULL_TREE;
16508 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16509 enum tree_code opcode = NOP_EXPR;
16510 if (code == OMP_ATOMIC_READ)
16511 {
16512 v = RECUR (TREE_OPERAND (op1, 0));
16513 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16514 }
16515 else if (code == OMP_ATOMIC_CAPTURE_OLD
16516 || code == OMP_ATOMIC_CAPTURE_NEW)
16517 {
16518 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16519 v = RECUR (TREE_OPERAND (op1, 0));
16520 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16521 if (TREE_CODE (op11) == COMPOUND_EXPR)
16522 {
16523 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16524 op11 = TREE_OPERAND (op11, 1);
16525 }
16526 lhs = RECUR (TREE_OPERAND (op11, 0));
16527 rhs = RECUR (TREE_OPERAND (op11, 1));
16528 opcode = TREE_CODE (op11);
16529 if (opcode == MODIFY_EXPR)
16530 opcode = NOP_EXPR;
16531 }
16532 else
16533 {
16534 code = OMP_ATOMIC;
16535 lhs = RECUR (TREE_OPERAND (op1, 0));
16536 rhs = RECUR (TREE_OPERAND (op1, 1));
16537 }
16538 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16539 OMP_ATOMIC_SEQ_CST (t));
16540 }
16541 break;
16542
16543 case TRANSACTION_EXPR:
16544 {
16545 int flags = 0;
16546 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16547 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16548
16549 if (TRANSACTION_EXPR_IS_STMT (t))
16550 {
16551 tree body = TRANSACTION_EXPR_BODY (t);
16552 tree noex = NULL_TREE;
16553 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16554 {
16555 noex = MUST_NOT_THROW_COND (body);
16556 if (noex == NULL_TREE)
16557 noex = boolean_true_node;
16558 body = TREE_OPERAND (body, 0);
16559 }
16560 stmt = begin_transaction_stmt (input_location, NULL, flags);
16561 RECUR (body);
16562 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16563 }
16564 else
16565 {
16566 stmt = build_transaction_expr (EXPR_LOCATION (t),
16567 RECUR (TRANSACTION_EXPR_BODY (t)),
16568 flags, NULL_TREE);
16569 RETURN (stmt);
16570 }
16571 }
16572 break;
16573
16574 case MUST_NOT_THROW_EXPR:
16575 {
16576 tree op0 = RECUR (TREE_OPERAND (t, 0));
16577 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16578 RETURN (build_must_not_throw_expr (op0, cond));
16579 }
16580
16581 case EXPR_PACK_EXPANSION:
16582 error ("invalid use of pack expansion expression");
16583 RETURN (error_mark_node);
16584
16585 case NONTYPE_ARGUMENT_PACK:
16586 error ("use %<...%> to expand argument pack");
16587 RETURN (error_mark_node);
16588
16589 case CILK_SPAWN_STMT:
16590 cfun->calls_cilk_spawn = 1;
16591 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16592
16593 case CILK_SYNC_STMT:
16594 RETURN (build_cilk_sync ());
16595
16596 case COMPOUND_EXPR:
16597 tmp = RECUR (TREE_OPERAND (t, 0));
16598 if (tmp == NULL_TREE)
16599 /* If the first operand was a statement, we're done with it. */
16600 RETURN (RECUR (TREE_OPERAND (t, 1)));
16601 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16602 RECUR (TREE_OPERAND (t, 1)),
16603 complain));
16604
16605 case ANNOTATE_EXPR:
16606 tmp = RECUR (TREE_OPERAND (t, 0));
16607 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16608 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16609
16610 default:
16611 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16612
16613 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16614 /*function_p=*/false,
16615 integral_constant_expression_p));
16616 }
16617
16618 RETURN (NULL_TREE);
16619 out:
16620 input_location = loc;
16621 return r;
16622#undef RECUR
16623#undef RETURN
16624}
16625
16626/* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16627 function. For description of the body see comment above
16628 cp_parser_omp_declare_reduction_exprs. */
16629
16630static void
16631tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16632{
16633 if (t == NULL_TREE || t == error_mark_node)
16634 return;
16635
16636 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16637
16638 tree_stmt_iterator tsi;
16639 int i;
16640 tree stmts[7];
16641 memset (stmts, 0, sizeof stmts);
16642 for (i = 0, tsi = tsi_start (t);
16643 i < 7 && !tsi_end_p (tsi);
16644 i++, tsi_next (&tsi))
16645 stmts[i] = tsi_stmt (tsi);
16646 gcc_assert (tsi_end_p (tsi));
16647
16648 if (i >= 3)
16649 {
16650 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16651 && TREE_CODE (stmts[1]) == DECL_EXPR);
16652 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16653 args, complain, in_decl);
16654 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16655 args, complain, in_decl);
16656 DECL_CONTEXT (omp_out) = current_function_decl;
16657 DECL_CONTEXT (omp_in) = current_function_decl;
16658 keep_next_level (true);
16659 tree block = begin_omp_structured_block ();
16660 tsubst_expr (stmts[2], args, complain, in_decl, false);
16661 block = finish_omp_structured_block (block);
16662 block = maybe_cleanup_point_expr_void (block);
16663 add_decl_expr (omp_out);
16664 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16665 TREE_NO_WARNING (omp_out) = 1;
16666 add_decl_expr (omp_in);
16667 finish_expr_stmt (block);
16668 }
16669 if (i >= 6)
16670 {
16671 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16672 && TREE_CODE (stmts[4]) == DECL_EXPR);
16673 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16674 args, complain, in_decl);
16675 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16676 args, complain, in_decl);
16677 DECL_CONTEXT (omp_priv) = current_function_decl;
16678 DECL_CONTEXT (omp_orig) = current_function_decl;
16679 keep_next_level (true);
16680 tree block = begin_omp_structured_block ();
16681 tsubst_expr (stmts[5], args, complain, in_decl, false);
16682 block = finish_omp_structured_block (block);
16683 block = maybe_cleanup_point_expr_void (block);
16684 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16685 add_decl_expr (omp_priv);
16686 add_decl_expr (omp_orig);
16687 finish_expr_stmt (block);
16688 if (i == 7)
16689 add_decl_expr (omp_orig);
16690 }
16691}
16692
16693/* T is a postfix-expression that is not being used in a function
16694 call. Return the substituted version of T. */
16695
16696static tree
16697tsubst_non_call_postfix_expression (tree t, tree args,
16698 tsubst_flags_t complain,
16699 tree in_decl)
16700{
16701 if (TREE_CODE (t) == SCOPE_REF)
16702 t = tsubst_qualified_id (t, args, complain, in_decl,
16703 /*done=*/false, /*address_p=*/false);
16704 else
16705 t = tsubst_copy_and_build (t, args, complain, in_decl,
16706 /*function_p=*/false,
16707 /*integral_constant_expression_p=*/false);
16708
16709 return t;
16710}
16711
16712/* Like tsubst but deals with expressions and performs semantic
16713 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16714
16715tree
16716tsubst_copy_and_build (tree t,
16717 tree args,
16718 tsubst_flags_t complain,
16719 tree in_decl,
16720 bool function_p,
16721 bool integral_constant_expression_p)
16722{
16723#define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16724#define RECUR(NODE) \
16725 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16726 /*function_p=*/false, \
16727 integral_constant_expression_p)
16728
16729 tree retval, op1;
16730 location_t loc;
16731
16732 if (t == NULL_TREE || t == error_mark_node)
16733 return t;
16734
16735 loc = input_location;
16736 if (EXPR_HAS_LOCATION (t))
16737 input_location = EXPR_LOCATION (t);
16738
16739 /* N3276 decltype magic only applies to calls at the top level or on the
16740 right side of a comma. */
16741 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16742 complain &= ~tf_decltype;
16743
16744 switch (TREE_CODE (t))
16745 {
16746 case USING_DECL:
16747 t = DECL_NAME (t);
16748 /* Fall through. */
16749 case IDENTIFIER_NODE:
16750 {
16751 tree decl;
16752 cp_id_kind idk;
16753 bool non_integral_constant_expression_p;
16754 const char *error_msg;
16755
16756 if (IDENTIFIER_TYPENAME_P (t))
16757 {
16758 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16759 t = mangle_conv_op_name_for_type (new_type);
16760 }
16761
16762 /* Look up the name. */
16763 decl = lookup_name (t);
16764
16765 /* By convention, expressions use ERROR_MARK_NODE to indicate
16766 failure, not NULL_TREE. */
16767 if (decl == NULL_TREE)
16768 decl = error_mark_node;
16769
16770 decl = finish_id_expression (t, decl, NULL_TREE,
16771 &idk,
16772 integral_constant_expression_p,
16773 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16774 &non_integral_constant_expression_p,
16775 /*template_p=*/false,
16776 /*done=*/true,
16777 /*address_p=*/false,
16778 /*template_arg_p=*/false,
16779 &error_msg,
16780 input_location);
16781 if (error_msg)
16782 error (error_msg);
16783 if (!function_p && identifier_p (decl))
16784 {
16785 if (complain & tf_error)
16786 unqualified_name_lookup_error (decl);
16787 decl = error_mark_node;
16788 }
16789 RETURN (decl);
16790 }
16791
16792 case TEMPLATE_ID_EXPR:
16793 {
16794 tree object;
16795 tree templ = RECUR (TREE_OPERAND (t, 0));
16796 tree targs = TREE_OPERAND (t, 1);
16797
16798 if (targs)
16799 targs = tsubst_template_args (targs, args, complain, in_decl);
16800 if (targs == error_mark_node)
16801 RETURN (error_mark_node);
16802
16803 if (TREE_CODE (templ) == SCOPE_REF)
16804 {
16805 tree name = TREE_OPERAND (templ, 1);
16806 tree tid = lookup_template_function (name, targs);
16807 TREE_OPERAND (templ, 1) = tid;
16808 RETURN (templ);
16809 }
16810
16811 if (variable_template_p (templ))
16812 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16813
16814 if (TREE_CODE (templ) == COMPONENT_REF)
16815 {
16816 object = TREE_OPERAND (templ, 0);
16817 templ = TREE_OPERAND (templ, 1);
16818 }
16819 else
16820 object = NULL_TREE;
16821 templ = lookup_template_function (templ, targs);
16822
16823 if (object)
16824 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16825 object, templ, NULL_TREE));
16826 else
16827 RETURN (baselink_for_fns (templ));
16828 }
16829
16830 case INDIRECT_REF:
16831 {
16832 tree r = RECUR (TREE_OPERAND (t, 0));
16833
16834 if (REFERENCE_REF_P (t))
16835 {
16836 /* A type conversion to reference type will be enclosed in
16837 such an indirect ref, but the substitution of the cast
16838 will have also added such an indirect ref. */
16839 r = convert_from_reference (r);
16840 }
16841 else
16842 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16843 complain|decltype_flag);
16844
16845 if (TREE_CODE (r) == INDIRECT_REF)
16846 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16847
16848 RETURN (r);
16849 }
16850
16851 case NOP_EXPR:
16852 {
16853 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16854 tree op0 = RECUR (TREE_OPERAND (t, 0));
16855 RETURN (build_nop (type, op0));
16856 }
16857
16858 case IMPLICIT_CONV_EXPR:
16859 {
16860 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16861 tree expr = RECUR (TREE_OPERAND (t, 0));
16862 int flags = LOOKUP_IMPLICIT;
16863 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16864 flags = LOOKUP_NORMAL;
16865 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16866 flags));
16867 }
16868
16869 case CONVERT_EXPR:
16870 {
16871 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16872 tree op0 = RECUR (TREE_OPERAND (t, 0));
16873 if (op0 == error_mark_node)
16874 RETURN (error_mark_node);
16875 RETURN (build1 (CONVERT_EXPR, type, op0));
16876 }
16877
16878 case CAST_EXPR:
16879 case REINTERPRET_CAST_EXPR:
16880 case CONST_CAST_EXPR:
16881 case DYNAMIC_CAST_EXPR:
16882 case STATIC_CAST_EXPR:
16883 {
16884 tree type;
16885 tree op, r = NULL_TREE;
16886
16887 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16888 if (integral_constant_expression_p
16889 && !cast_valid_in_integral_constant_expression_p (type))
16890 {
16891 if (complain & tf_error)
16892 error ("a cast to a type other than an integral or "
16893 "enumeration type cannot appear in a constant-expression");
16894 RETURN (error_mark_node);
16895 }
16896
16897 op = RECUR (TREE_OPERAND (t, 0));
16898
16899 warning_sentinel s(warn_useless_cast);
16900 switch (TREE_CODE (t))
16901 {
16902 case CAST_EXPR:
16903 r = build_functional_cast (type, op, complain);
16904 break;
16905 case REINTERPRET_CAST_EXPR:
16906 r = build_reinterpret_cast (type, op, complain);
16907 break;
16908 case CONST_CAST_EXPR:
16909 r = build_const_cast (type, op, complain);
16910 break;
16911 case DYNAMIC_CAST_EXPR:
16912 r = build_dynamic_cast (type, op, complain);
16913 break;
16914 case STATIC_CAST_EXPR:
16915 r = build_static_cast (type, op, complain);
16916 break;
16917 default:
16918 gcc_unreachable ();
16919 }
16920
16921 RETURN (r);
16922 }
16923
16924 case POSTDECREMENT_EXPR:
16925 case POSTINCREMENT_EXPR:
16926 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16927 args, complain, in_decl);
16928 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16929 complain|decltype_flag));
16930
16931 case PREDECREMENT_EXPR:
16932 case PREINCREMENT_EXPR:
16933 case NEGATE_EXPR:
16934 case BIT_NOT_EXPR:
16935 case ABS_EXPR:
16936 case TRUTH_NOT_EXPR:
16937 case UNARY_PLUS_EXPR: /* Unary + */
16938 case REALPART_EXPR:
16939 case IMAGPART_EXPR:
16940 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16941 RECUR (TREE_OPERAND (t, 0)),
16942 complain|decltype_flag));
16943
16944 case FIX_TRUNC_EXPR:
16945 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16946 false, complain));
16947
16948 case ADDR_EXPR:
16949 op1 = TREE_OPERAND (t, 0);
16950 if (TREE_CODE (op1) == LABEL_DECL)
16951 RETURN (finish_label_address_expr (DECL_NAME (op1),
16952 EXPR_LOCATION (op1)));
16953 if (TREE_CODE (op1) == SCOPE_REF)
16954 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16955 /*done=*/true, /*address_p=*/true);
16956 else
16957 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16958 in_decl);
16959 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16960 complain|decltype_flag));
16961
16962 case PLUS_EXPR:
16963 case MINUS_EXPR:
16964 case MULT_EXPR:
16965 case TRUNC_DIV_EXPR:
16966 case CEIL_DIV_EXPR:
16967 case FLOOR_DIV_EXPR:
16968 case ROUND_DIV_EXPR:
16969 case EXACT_DIV_EXPR:
16970 case BIT_AND_EXPR:
16971 case BIT_IOR_EXPR:
16972 case BIT_XOR_EXPR:
16973 case TRUNC_MOD_EXPR:
16974 case FLOOR_MOD_EXPR:
16975 case TRUTH_ANDIF_EXPR:
16976 case TRUTH_ORIF_EXPR:
16977 case TRUTH_AND_EXPR:
16978 case TRUTH_OR_EXPR:
16979 case RSHIFT_EXPR:
16980 case LSHIFT_EXPR:
16981 case RROTATE_EXPR:
16982 case LROTATE_EXPR:
16983 case EQ_EXPR:
16984 case NE_EXPR:
16985 case MAX_EXPR:
16986 case MIN_EXPR:
16987 case LE_EXPR:
16988 case GE_EXPR:
16989 case LT_EXPR:
16990 case GT_EXPR:
16991 case MEMBER_REF:
16992 case DOTSTAR_EXPR:
16993 {
16994 warning_sentinel s1(warn_type_limits);
16995 warning_sentinel s2(warn_div_by_zero);
16996 warning_sentinel s3(warn_logical_op);
16997 warning_sentinel s4(warn_tautological_compare);
16998 tree op0 = RECUR (TREE_OPERAND (t, 0));
16999 tree op1 = RECUR (TREE_OPERAND (t, 1));
17000 tree r = build_x_binary_op
17001 (input_location, TREE_CODE (t),
17002 op0,
17003 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17004 ? ERROR_MARK
17005 : TREE_CODE (TREE_OPERAND (t, 0))),
17006 op1,
17007 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17008 ? ERROR_MARK
17009 : TREE_CODE (TREE_OPERAND (t, 1))),
17010 /*overload=*/NULL,
17011 complain|decltype_flag);
17012 if (EXPR_P (r) && TREE_NO_WARNING (t))
17013 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17014
17015 RETURN (r);
17016 }
17017
17018 case POINTER_PLUS_EXPR:
17019 {
17020 tree op0 = RECUR (TREE_OPERAND (t, 0));
17021 tree op1 = RECUR (TREE_OPERAND (t, 1));
17022 RETURN (fold_build_pointer_plus (op0, op1));
17023 }
17024
17025 case SCOPE_REF:
17026 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17027 /*address_p=*/false));
17028 case ARRAY_REF:
17029 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17030 args, complain, in_decl);
17031 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17032 RECUR (TREE_OPERAND (t, 1)),
17033 complain|decltype_flag));
17034
17035 case ARRAY_NOTATION_REF:
17036 {
17037 tree start_index, length, stride;
17038 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17039 args, complain, in_decl);
17040 start_index = RECUR (ARRAY_NOTATION_START (t));
17041 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17042 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17043 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17044 length, stride, TREE_TYPE (op1)));
17045 }
17046 case SIZEOF_EXPR:
17047 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17048 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17049 RETURN (tsubst_copy (t, args, complain, in_decl));
17050 /* Fall through */
17051
17052 case ALIGNOF_EXPR:
17053 {
17054 tree r;
17055
17056 op1 = TREE_OPERAND (t, 0);
17057 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17058 op1 = TREE_TYPE (op1);
17059 if (!args)
17060 {
17061 /* When there are no ARGS, we are trying to evaluate a
17062 non-dependent expression from the parser. Trying to do
17063 the substitutions may not work. */
17064 if (!TYPE_P (op1))
17065 op1 = TREE_TYPE (op1);
17066 }
17067 else
17068 {
17069 ++cp_unevaluated_operand;
17070 ++c_inhibit_evaluation_warnings;
17071 if (TYPE_P (op1))
17072 op1 = tsubst (op1, args, complain, in_decl);
17073 else
17074 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17075 /*function_p=*/false,
17076 /*integral_constant_expression_p=*/
17077 false);
17078 --cp_unevaluated_operand;
17079 --c_inhibit_evaluation_warnings;
17080 }
17081 if (TYPE_P (op1))
17082 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17083 complain & tf_error);
17084 else
17085 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17086 complain & tf_error);
17087 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17088 {
17089 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17090 {
17091 if (!processing_template_decl && TYPE_P (op1))
17092 {
17093 r = build_min (SIZEOF_EXPR, size_type_node,
17094 build1 (NOP_EXPR, op1, error_mark_node));
17095 SIZEOF_EXPR_TYPE_P (r) = 1;
17096 }
17097 else
17098 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17099 TREE_SIDE_EFFECTS (r) = 0;
17100 TREE_READONLY (r) = 1;
17101 }
17102 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17103 }
17104 RETURN (r);
17105 }
17106
17107 case AT_ENCODE_EXPR:
17108 {
17109 op1 = TREE_OPERAND (t, 0);
17110 ++cp_unevaluated_operand;
17111 ++c_inhibit_evaluation_warnings;
17112 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17113 /*function_p=*/false,
17114 /*integral_constant_expression_p=*/false);
17115 --cp_unevaluated_operand;
17116 --c_inhibit_evaluation_warnings;
17117 RETURN (objc_build_encode_expr (op1));
17118 }
17119
17120 case NOEXCEPT_EXPR:
17121 op1 = TREE_OPERAND (t, 0);
17122 ++cp_unevaluated_operand;
17123 ++c_inhibit_evaluation_warnings;
17124 ++cp_noexcept_operand;
17125 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17126 /*function_p=*/false,
17127 /*integral_constant_expression_p=*/false);
17128 --cp_unevaluated_operand;
17129 --c_inhibit_evaluation_warnings;
17130 --cp_noexcept_operand;
17131 RETURN (finish_noexcept_expr (op1, complain));
17132
17133 case MODOP_EXPR:
17134 {
17135 warning_sentinel s(warn_div_by_zero);
17136 tree lhs = RECUR (TREE_OPERAND (t, 0));
17137 tree rhs = RECUR (TREE_OPERAND (t, 2));
17138 tree r = build_x_modify_expr
17139 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17140 complain|decltype_flag);
17141 /* TREE_NO_WARNING must be set if either the expression was
17142 parenthesized or it uses an operator such as >>= rather
17143 than plain assignment. In the former case, it was already
17144 set and must be copied. In the latter case,
17145 build_x_modify_expr sets it and it must not be reset
17146 here. */
17147 if (TREE_NO_WARNING (t))
17148 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17149
17150 RETURN (r);
17151 }
17152
17153 case ARROW_EXPR:
17154 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17155 args, complain, in_decl);
17156 /* Remember that there was a reference to this entity. */
17157 if (DECL_P (op1)
17158 && !mark_used (op1, complain) && !(complain & tf_error))
17159 RETURN (error_mark_node);
17160 RETURN (build_x_arrow (input_location, op1, complain));
17161
17162 case NEW_EXPR:
17163 {
17164 tree placement = RECUR (TREE_OPERAND (t, 0));
17165 tree init = RECUR (TREE_OPERAND (t, 3));
17166 vec<tree, va_gc> *placement_vec;
17167 vec<tree, va_gc> *init_vec;
17168 tree ret;
17169
17170 if (placement == NULL_TREE)
17171 placement_vec = NULL;
17172 else
17173 {
17174 placement_vec = make_tree_vector ();
17175 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17176 vec_safe_push (placement_vec, TREE_VALUE (placement));
17177 }
17178
17179 /* If there was an initializer in the original tree, but it
17180 instantiated to an empty list, then we should pass a
17181 non-NULL empty vector to tell build_new that it was an
17182 empty initializer() rather than no initializer. This can
17183 only happen when the initializer is a pack expansion whose
17184 parameter packs are of length zero. */
17185 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17186 init_vec = NULL;
17187 else
17188 {
17189 init_vec = make_tree_vector ();
17190 if (init == void_node)
17191 gcc_assert (init_vec != NULL);
17192 else
17193 {
17194 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17195 vec_safe_push (init_vec, TREE_VALUE (init));
17196 }
17197 }
17198
17199 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17200 tree op2 = RECUR (TREE_OPERAND (t, 2));
17201 ret = build_new (&placement_vec, op1, op2, &init_vec,
17202 NEW_EXPR_USE_GLOBAL (t),
17203 complain);
17204
17205 if (placement_vec != NULL)
17206 release_tree_vector (placement_vec);
17207 if (init_vec != NULL)
17208 release_tree_vector (init_vec);
17209
17210 RETURN (ret);
17211 }
17212
17213 case DELETE_EXPR:
17214 {
17215 tree op0 = RECUR (TREE_OPERAND (t, 0));
17216 tree op1 = RECUR (TREE_OPERAND (t, 1));
17217 RETURN (delete_sanity (op0, op1,
17218 DELETE_EXPR_USE_VEC (t),
17219 DELETE_EXPR_USE_GLOBAL (t),
17220 complain));
17221 }
17222
17223 case COMPOUND_EXPR:
17224 {
17225 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17226 complain & ~tf_decltype, in_decl,
17227 /*function_p=*/false,
17228 integral_constant_expression_p);
17229 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17230 op0,
17231 RECUR (TREE_OPERAND (t, 1)),
17232 complain|decltype_flag));
17233 }
17234
17235 case CALL_EXPR:
17236 {
17237 tree function;
17238 vec<tree, va_gc> *call_args;
17239 unsigned int nargs, i;
17240 bool qualified_p;
17241 bool koenig_p;
17242 tree ret;
17243
17244 function = CALL_EXPR_FN (t);
17245 /* Internal function with no arguments. */
17246 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17247 RETURN (t);
17248
17249 /* When we parsed the expression, we determined whether or
17250 not Koenig lookup should be performed. */
17251 koenig_p = KOENIG_LOOKUP_P (t);
17252 if (function == NULL_TREE)
17253 {
17254 koenig_p = false;
17255 qualified_p = false;
17256 }
17257 else if (TREE_CODE (function) == SCOPE_REF)
17258 {
17259 qualified_p = true;
17260 function = tsubst_qualified_id (function, args, complain, in_decl,
17261 /*done=*/false,
17262 /*address_p=*/false);
17263 }
17264 else if (koenig_p && identifier_p (function))
17265 {
17266 /* Do nothing; calling tsubst_copy_and_build on an identifier
17267 would incorrectly perform unqualified lookup again.
17268
17269 Note that we can also have an IDENTIFIER_NODE if the earlier
17270 unqualified lookup found a member function; in that case
17271 koenig_p will be false and we do want to do the lookup
17272 again to find the instantiated member function.
17273
17274 FIXME but doing that causes c++/15272, so we need to stop
17275 using IDENTIFIER_NODE in that situation. */
17276 qualified_p = false;
17277 }
17278 else
17279 {
17280 if (TREE_CODE (function) == COMPONENT_REF)
17281 {
17282 tree op = TREE_OPERAND (function, 1);
17283
17284 qualified_p = (TREE_CODE (op) == SCOPE_REF
17285 || (BASELINK_P (op)
17286 && BASELINK_QUALIFIED_P (op)));
17287 }
17288 else
17289 qualified_p = false;
17290
17291 if (TREE_CODE (function) == ADDR_EXPR
17292 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17293 /* Avoid error about taking the address of a constructor. */
17294 function = TREE_OPERAND (function, 0);
17295
17296 function = tsubst_copy_and_build (function, args, complain,
17297 in_decl,
17298 !qualified_p,
17299 integral_constant_expression_p);
17300
17301 if (BASELINK_P (function))
17302 qualified_p = true;
17303 }
17304
17305 nargs = call_expr_nargs (t);
17306 call_args = make_tree_vector ();
17307 for (i = 0; i < nargs; ++i)
17308 {
17309 tree arg = CALL_EXPR_ARG (t, i);
17310
17311 if (!PACK_EXPANSION_P (arg))
17312 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17313 else
17314 {
17315 /* Expand the pack expansion and push each entry onto
17316 CALL_ARGS. */
17317 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17318 if (TREE_CODE (arg) == TREE_VEC)
17319 {
17320 unsigned int len, j;
17321
17322 len = TREE_VEC_LENGTH (arg);
17323 for (j = 0; j < len; ++j)
17324 {
17325 tree value = TREE_VEC_ELT (arg, j);
17326 if (value != NULL_TREE)
17327 value = convert_from_reference (value);
17328 vec_safe_push (call_args, value);
17329 }
17330 }
17331 else
17332 {
17333 /* A partial substitution. Add one entry. */
17334 vec_safe_push (call_args, arg);
17335 }
17336 }
17337 }
17338
17339 /* We do not perform argument-dependent lookup if normal
17340 lookup finds a non-function, in accordance with the
17341 expected resolution of DR 218. */
17342 if (koenig_p
17343 && ((is_overloaded_fn (function)
17344 /* If lookup found a member function, the Koenig lookup is
17345 not appropriate, even if an unqualified-name was used
17346 to denote the function. */
17347 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17348 || identifier_p (function))
17349 /* Only do this when substitution turns a dependent call
17350 into a non-dependent call. */
17351 && type_dependent_expression_p_push (t)
17352 && !any_type_dependent_arguments_p (call_args))
17353 function = perform_koenig_lookup (function, call_args, tf_none);
17354
17355 if (function != NULL_TREE
17356 && identifier_p (function)
17357 && !any_type_dependent_arguments_p (call_args))
17358 {
17359 if (koenig_p && (complain & tf_warning_or_error))
17360 {
17361 /* For backwards compatibility and good diagnostics, try
17362 the unqualified lookup again if we aren't in SFINAE
17363 context. */
17364 tree unq = (tsubst_copy_and_build
17365 (function, args, complain, in_decl, true,
17366 integral_constant_expression_p));
17367 if (unq == error_mark_node)
17368 {
17369 release_tree_vector (call_args);
17370 RETURN (error_mark_node);
17371 }
17372
17373 if (unq != function)
17374 {
17375 /* In a lambda fn, we have to be careful to not
17376 introduce new this captures. Legacy code can't
17377 be using lambdas anyway, so it's ok to be
17378 stricter. */
17379 bool in_lambda = (current_class_type
17380 && LAMBDA_TYPE_P (current_class_type));
17381 char const *const msg
17382 = G_("%qD was not declared in this scope, "
17383 "and no declarations were found by "
17384 "argument-dependent lookup at the point "
17385 "of instantiation");
17386
17387 bool diag = true;
17388 if (in_lambda)
17389 error_at (EXPR_LOC_OR_LOC (t, input_location),
17390 msg, function);
17391 else
17392 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17393 msg, function);
17394 if (diag)
17395 {
17396 tree fn = unq;
17397 if (INDIRECT_REF_P (fn))
17398 fn = TREE_OPERAND (fn, 0);
17399 if (TREE_CODE (fn) == COMPONENT_REF)
17400 fn = TREE_OPERAND (fn, 1);
17401 if (is_overloaded_fn (fn))
17402 fn = get_first_fn (fn);
17403
17404 if (!DECL_P (fn))
17405 /* Can't say anything more. */;
17406 else if (DECL_CLASS_SCOPE_P (fn))
17407 {
17408 location_t loc = EXPR_LOC_OR_LOC (t,
17409 input_location);
17410 inform (loc,
17411 "declarations in dependent base %qT are "
17412 "not found by unqualified lookup",
17413 DECL_CLASS_CONTEXT (fn));
17414 if (current_class_ptr)
17415 inform (loc,
17416 "use %<this->%D%> instead", function);
17417 else
17418 inform (loc,
17419 "use %<%T::%D%> instead",
17420 current_class_name, function);
17421 }
17422 else
17423 inform (DECL_SOURCE_LOCATION (fn),
17424 "%qD declared here, later in the "
17425 "translation unit", fn);
17426 if (in_lambda)
17427 {
17428 release_tree_vector (call_args);
17429 RETURN (error_mark_node);
17430 }
17431 }
17432
17433 function = unq;
17434 }
17435 }
17436 if (identifier_p (function))
17437 {
17438 if (complain & tf_error)
17439 unqualified_name_lookup_error (function);
17440 release_tree_vector (call_args);
17441 RETURN (error_mark_node);
17442 }
17443 }
17444
17445 /* Remember that there was a reference to this entity. */
17446 if (function != NULL_TREE
17447 && DECL_P (function)
17448 && !mark_used (function, complain) && !(complain & tf_error))
17449 {
17450 release_tree_vector (call_args);
17451 RETURN (error_mark_node);
17452 }
17453
17454 /* Put back tf_decltype for the actual call. */
17455 complain |= decltype_flag;
17456
17457 if (function == NULL_TREE)
17458 switch (CALL_EXPR_IFN (t))
17459 {
17460 case IFN_LAUNDER:
17461 gcc_assert (nargs == 1);
17462 if (vec_safe_length (call_args) != 1)
17463 {
17464 error_at (EXPR_LOC_OR_LOC (t, input_location),
17465 "wrong number of arguments to "
17466 "%<__builtin_launder%>");
17467 ret = error_mark_node;
17468 }
17469 else
17470 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17471 input_location),
17472 (*call_args)[0], complain);
17473 break;
17474
17475 default:
17476 /* Unsupported internal function with arguments. */
17477 gcc_unreachable ();
17478 }
17479 else if (TREE_CODE (function) == OFFSET_REF)
17480 ret = build_offset_ref_call_from_tree (function, &call_args,
17481 complain);
17482 else if (TREE_CODE (function) == COMPONENT_REF)
17483 {
17484 tree instance = TREE_OPERAND (function, 0);
17485 tree fn = TREE_OPERAND (function, 1);
17486
17487 if (processing_template_decl
17488 && (type_dependent_expression_p (instance)
17489 || (!BASELINK_P (fn)
17490 && TREE_CODE (fn) != FIELD_DECL)
17491 || type_dependent_expression_p (fn)
17492 || any_type_dependent_arguments_p (call_args)))
17493 ret = build_nt_call_vec (function, call_args);
17494 else if (!BASELINK_P (fn))
17495 ret = finish_call_expr (function, &call_args,
17496 /*disallow_virtual=*/false,
17497 /*koenig_p=*/false,
17498 complain);
17499 else
17500 ret = (build_new_method_call
17501 (instance, fn,
17502 &call_args, NULL_TREE,
17503 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17504 /*fn_p=*/NULL,
17505 complain));
17506 }
17507 else
17508 ret = finish_call_expr (function, &call_args,
17509 /*disallow_virtual=*/qualified_p,
17510 koenig_p,
17511 complain);
17512
17513 release_tree_vector (call_args);
17514
17515 if (ret != error_mark_node)
17516 {
17517 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17518 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17519 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17520 bool thk = CALL_FROM_THUNK_P (t);
17521 if (op || ord || rev || thk)
17522 {
17523 function = extract_call_expr (ret);
17524 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17525 CALL_EXPR_ORDERED_ARGS (function) = ord;
17526 CALL_EXPR_REVERSE_ARGS (function) = rev;
17527 if (thk)
17528 {
17529 if (TREE_CODE (function) == CALL_EXPR)
17530 CALL_FROM_THUNK_P (function) = true;
17531 else
17532 AGGR_INIT_FROM_THUNK_P (function) = true;
17533 /* The thunk location is not interesting. */
17534 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17535 }
17536 }
17537 }
17538
17539 RETURN (ret);
17540 }
17541
17542 case COND_EXPR:
17543 {
17544 tree cond = RECUR (TREE_OPERAND (t, 0));
17545 tree folded_cond = fold_non_dependent_expr (cond);
17546 tree exp1, exp2;
17547
17548 if (TREE_CODE (folded_cond) == INTEGER_CST)
17549 {
17550 if (integer_zerop (folded_cond))
17551 {
17552 ++c_inhibit_evaluation_warnings;
17553 exp1 = RECUR (TREE_OPERAND (t, 1));
17554 --c_inhibit_evaluation_warnings;
17555 exp2 = RECUR (TREE_OPERAND (t, 2));
17556 }
17557 else
17558 {
17559 exp1 = RECUR (TREE_OPERAND (t, 1));
17560 ++c_inhibit_evaluation_warnings;
17561 exp2 = RECUR (TREE_OPERAND (t, 2));
17562 --c_inhibit_evaluation_warnings;
17563 }
17564 cond = folded_cond;
17565 }
17566 else
17567 {
17568 exp1 = RECUR (TREE_OPERAND (t, 1));
17569 exp2 = RECUR (TREE_OPERAND (t, 2));
17570 }
17571
17572 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17573 cond, exp1, exp2, complain));
17574 }
17575
17576 case PSEUDO_DTOR_EXPR:
17577 {
17578 tree op0 = RECUR (TREE_OPERAND (t, 0));
17579 tree op1 = RECUR (TREE_OPERAND (t, 1));
17580 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17581 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17582 input_location));
17583 }
17584
17585 case TREE_LIST:
17586 {
17587 tree purpose, value, chain;
17588
17589 if (t == void_list_node)
17590 RETURN (t);
17591
17592 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17593 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17594 {
17595 /* We have pack expansions, so expand those and
17596 create a new list out of it. */
17597 tree purposevec = NULL_TREE;
17598 tree valuevec = NULL_TREE;
17599 tree chain;
17600 int i, len = -1;
17601
17602 /* Expand the argument expressions. */
17603 if (TREE_PURPOSE (t))
17604 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17605 complain, in_decl);
17606 if (TREE_VALUE (t))
17607 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17608 complain, in_decl);
17609
17610 /* Build the rest of the list. */
17611 chain = TREE_CHAIN (t);
17612 if (chain && chain != void_type_node)
17613 chain = RECUR (chain);
17614
17615 /* Determine the number of arguments. */
17616 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17617 {
17618 len = TREE_VEC_LENGTH (purposevec);
17619 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17620 }
17621 else if (TREE_CODE (valuevec) == TREE_VEC)
17622 len = TREE_VEC_LENGTH (valuevec);
17623 else
17624 {
17625 /* Since we only performed a partial substitution into
17626 the argument pack, we only RETURN (a single list
17627 node. */
17628 if (purposevec == TREE_PURPOSE (t)
17629 && valuevec == TREE_VALUE (t)
17630 && chain == TREE_CHAIN (t))
17631 RETURN (t);
17632
17633 RETURN (tree_cons (purposevec, valuevec, chain));
17634 }
17635
17636 /* Convert the argument vectors into a TREE_LIST */
17637 i = len;
17638 while (i > 0)
17639 {
17640 /* Grab the Ith values. */
17641 i--;
17642 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17643 : NULL_TREE;
17644 value
17645 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17646 : NULL_TREE;
17647
17648 /* Build the list (backwards). */
17649 chain = tree_cons (purpose, value, chain);
17650 }
17651
17652 RETURN (chain);
17653 }
17654
17655 purpose = TREE_PURPOSE (t);
17656 if (purpose)
17657 purpose = RECUR (purpose);
17658 value = TREE_VALUE (t);
17659 if (value)
17660 value = RECUR (value);
17661 chain = TREE_CHAIN (t);
17662 if (chain && chain != void_type_node)
17663 chain = RECUR (chain);
17664 if (purpose == TREE_PURPOSE (t)
17665 && value == TREE_VALUE (t)
17666 && chain == TREE_CHAIN (t))
17667 RETURN (t);
17668 RETURN (tree_cons (purpose, value, chain));
17669 }
17670
17671 case COMPONENT_REF:
17672 {
17673 tree object;
17674 tree object_type;
17675 tree member;
17676 tree r;
17677
17678 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17679 args, complain, in_decl);
17680 /* Remember that there was a reference to this entity. */
17681 if (DECL_P (object)
17682 && !mark_used (object, complain) && !(complain & tf_error))
17683 RETURN (error_mark_node);
17684 object_type = TREE_TYPE (object);
17685
17686 member = TREE_OPERAND (t, 1);
17687 if (BASELINK_P (member))
17688 member = tsubst_baselink (member,
17689 non_reference (TREE_TYPE (object)),
17690 args, complain, in_decl);
17691 else
17692 member = tsubst_copy (member, args, complain, in_decl);
17693 if (member == error_mark_node)
17694 RETURN (error_mark_node);
17695
17696 if (TREE_CODE (member) == FIELD_DECL)
17697 {
17698 r = finish_non_static_data_member (member, object, NULL_TREE);
17699 if (TREE_CODE (r) == COMPONENT_REF)
17700 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17701 RETURN (r);
17702 }
17703 else if (type_dependent_expression_p (object))
17704 /* We can't do much here. */;
17705 else if (!CLASS_TYPE_P (object_type))
17706 {
17707 if (scalarish_type_p (object_type))
17708 {
17709 tree s = NULL_TREE;
17710 tree dtor = member;
17711
17712 if (TREE_CODE (dtor) == SCOPE_REF)
17713 {
17714 s = TREE_OPERAND (dtor, 0);
17715 dtor = TREE_OPERAND (dtor, 1);
17716 }
17717 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17718 {
17719 dtor = TREE_OPERAND (dtor, 0);
17720 if (TYPE_P (dtor))
17721 RETURN (finish_pseudo_destructor_expr
17722 (object, s, dtor, input_location));
17723 }
17724 }
17725 }
17726 else if (TREE_CODE (member) == SCOPE_REF
17727 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17728 {
17729 /* Lookup the template functions now that we know what the
17730 scope is. */
17731 tree scope = TREE_OPERAND (member, 0);
17732 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17733 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17734 member = lookup_qualified_name (scope, tmpl,
17735 /*is_type_p=*/false,
17736 /*complain=*/false);
17737 if (BASELINK_P (member))
17738 {
17739 BASELINK_FUNCTIONS (member)
17740 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17741 args);
17742 member = (adjust_result_of_qualified_name_lookup
17743 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17744 object_type));
17745 }
17746 else
17747 {
17748 qualified_name_lookup_error (scope, tmpl, member,
17749 input_location);
17750 RETURN (error_mark_node);
17751 }
17752 }
17753 else if (TREE_CODE (member) == SCOPE_REF
17754 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17755 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17756 {
17757 if (complain & tf_error)
17758 {
17759 if (TYPE_P (TREE_OPERAND (member, 0)))
17760 error ("%qT is not a class or namespace",
17761 TREE_OPERAND (member, 0));
17762 else
17763 error ("%qD is not a class or namespace",
17764 TREE_OPERAND (member, 0));
17765 }
17766 RETURN (error_mark_node);
17767 }
17768
17769 r = finish_class_member_access_expr (object, member,
17770 /*template_p=*/false,
17771 complain);
17772 if (TREE_CODE (r) == COMPONENT_REF)
17773 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17774 RETURN (r);
17775 }
17776
17777 case THROW_EXPR:
17778 RETURN (build_throw
17779 (RECUR (TREE_OPERAND (t, 0))));
17780
17781 case CONSTRUCTOR:
17782 {
17783 vec<constructor_elt, va_gc> *n;
17784 constructor_elt *ce;
17785 unsigned HOST_WIDE_INT idx;
17786 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17787 bool process_index_p;
17788 int newlen;
17789 bool need_copy_p = false;
17790 tree r;
17791
17792 if (type == error_mark_node)
17793 RETURN (error_mark_node);
17794
17795 /* digest_init will do the wrong thing if we let it. */
17796 if (type && TYPE_PTRMEMFUNC_P (type))
17797 RETURN (t);
17798
17799 /* We do not want to process the index of aggregate
17800 initializers as they are identifier nodes which will be
17801 looked up by digest_init. */
17802 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17803
17804 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17805 newlen = vec_safe_length (n);
17806 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17807 {
17808 if (ce->index && process_index_p
17809 /* An identifier index is looked up in the type
17810 being initialized, not the current scope. */
17811 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17812 ce->index = RECUR (ce->index);
17813
17814 if (PACK_EXPANSION_P (ce->value))
17815 {
17816 /* Substitute into the pack expansion. */
17817 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17818 in_decl);
17819
17820 if (ce->value == error_mark_node
17821 || PACK_EXPANSION_P (ce->value))
17822 ;
17823 else if (TREE_VEC_LENGTH (ce->value) == 1)
17824 /* Just move the argument into place. */
17825 ce->value = TREE_VEC_ELT (ce->value, 0);
17826 else
17827 {
17828 /* Update the length of the final CONSTRUCTOR
17829 arguments vector, and note that we will need to
17830 copy.*/
17831 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17832 need_copy_p = true;
17833 }
17834 }
17835 else
17836 ce->value = RECUR (ce->value);
17837 }
17838
17839 if (need_copy_p)
17840 {
17841 vec<constructor_elt, va_gc> *old_n = n;
17842
17843 vec_alloc (n, newlen);
17844 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17845 {
17846 if (TREE_CODE (ce->value) == TREE_VEC)
17847 {
17848 int i, len = TREE_VEC_LENGTH (ce->value);
17849 for (i = 0; i < len; ++i)
17850 CONSTRUCTOR_APPEND_ELT (n, 0,
17851 TREE_VEC_ELT (ce->value, i));
17852 }
17853 else
17854 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17855 }
17856 }
17857
17858 r = build_constructor (init_list_type_node, n);
17859 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17860
17861 if (TREE_HAS_CONSTRUCTOR (t))
17862 RETURN (finish_compound_literal (type, r, complain));
17863
17864 TREE_TYPE (r) = type;
17865 RETURN (r);
17866 }
17867
17868 case TYPEID_EXPR:
17869 {
17870 tree operand_0 = TREE_OPERAND (t, 0);
17871 if (TYPE_P (operand_0))
17872 {
17873 operand_0 = tsubst (operand_0, args, complain, in_decl);
17874 RETURN (get_typeid (operand_0, complain));
17875 }
17876 else
17877 {
17878 operand_0 = RECUR (operand_0);
17879 RETURN (build_typeid (operand_0, complain));
17880 }
17881 }
17882
17883 case VAR_DECL:
17884 if (!args)
17885 RETURN (t);
17886 else if (DECL_PACK_P (t))
17887 {
17888 /* We don't build decls for an instantiation of a
17889 variadic capture proxy, we instantiate the elements
17890 when needed. */
17891 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17892 return RECUR (DECL_VALUE_EXPR (t));
17893 }
17894 /* Fall through */
17895
17896 case PARM_DECL:
17897 {
17898 tree r = tsubst_copy (t, args, complain, in_decl);
17899 /* ??? We're doing a subset of finish_id_expression here. */
17900 if (VAR_P (r)
17901 && !processing_template_decl
17902 && !cp_unevaluated_operand
17903 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17904 && CP_DECL_THREAD_LOCAL_P (r))
17905 {
17906 if (tree wrap = get_tls_wrapper_fn (r))
17907 /* Replace an evaluated use of the thread_local variable with
17908 a call to its wrapper. */
17909 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17910 }
17911 else if (outer_automatic_var_p (r))
17912 {
17913 r = process_outer_var_ref (r, complain);
17914 if (is_capture_proxy (r))
17915 register_local_specialization (r, t);
17916 }
17917
17918 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17919 /* If the original type was a reference, we'll be wrapped in
17920 the appropriate INDIRECT_REF. */
17921 r = convert_from_reference (r);
17922 RETURN (r);
17923 }
17924
17925 case VA_ARG_EXPR:
17926 {
17927 tree op0 = RECUR (TREE_OPERAND (t, 0));
17928 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17929 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17930 }
17931
17932 case OFFSETOF_EXPR:
17933 {
17934 tree object_ptr
17935 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17936 in_decl, /*function_p=*/false,
17937 /*integral_constant_expression_p=*/false);
17938 RETURN (finish_offsetof (object_ptr,
17939 RECUR (TREE_OPERAND (t, 0)),
17940 EXPR_LOCATION (t)));
17941 }
17942
17943 case ADDRESSOF_EXPR:
17944 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17945 RECUR (TREE_OPERAND (t, 0)), complain));
17946
17947 case TRAIT_EXPR:
17948 {
17949 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17950 complain, in_decl);
17951
17952 tree type2 = TRAIT_EXPR_TYPE2 (t);
17953 if (type2 && TREE_CODE (type2) == TREE_LIST)
17954 type2 = RECUR (type2);
17955 else if (type2)
17956 type2 = tsubst (type2, args, complain, in_decl);
17957
17958 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17959 }
17960
17961 case STMT_EXPR:
17962 {
17963 tree old_stmt_expr = cur_stmt_expr;
17964 tree stmt_expr = begin_stmt_expr ();
17965
17966 cur_stmt_expr = stmt_expr;
17967 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17968 integral_constant_expression_p);
17969 stmt_expr = finish_stmt_expr (stmt_expr, false);
17970 cur_stmt_expr = old_stmt_expr;
17971
17972 /* If the resulting list of expression statement is empty,
17973 fold it further into void_node. */
17974 if (empty_expr_stmt_p (stmt_expr))
17975 stmt_expr = void_node;
17976
17977 RETURN (stmt_expr);
17978 }
17979
17980 case LAMBDA_EXPR:
17981 {
17982 tree r = build_lambda_expr ();
17983
17984 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17985 LAMBDA_EXPR_CLOSURE (r) = type;
17986 CLASSTYPE_LAMBDA_EXPR (type) = r;
17987
17988 LAMBDA_EXPR_LOCATION (r)
17989 = LAMBDA_EXPR_LOCATION (t);
17990 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17991 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17992 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17993 LAMBDA_EXPR_DISCRIMINATOR (r)
17994 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17995 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17996 if (!scope)
17997 /* No substitution needed. */;
17998 else if (VAR_OR_FUNCTION_DECL_P (scope))
17999 /* For a function or variable scope, we want to use tsubst so that we
18000 don't complain about referring to an auto before deduction. */
18001 scope = tsubst (scope, args, complain, in_decl);
18002 else if (TREE_CODE (scope) == PARM_DECL)
18003 {
18004 /* Look up the parameter we want directly, as tsubst_copy
18005 doesn't do what we need. */
18006 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
18007 tree parm = FUNCTION_FIRST_USER_PARM (fn);
18008 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
18009 parm = DECL_CHAIN (parm);
18010 scope = parm;
18011 /* FIXME Work around the parm not having DECL_CONTEXT set. */
18012 if (DECL_CONTEXT (scope) == NULL_TREE)
18013 DECL_CONTEXT (scope) = fn;
18014 }
18015 else if (TREE_CODE (scope) == FIELD_DECL)
18016 /* For a field, use tsubst_copy so that we look up the existing field
18017 rather than build a new one. */
18018 scope = RECUR (scope);
18019 else
18020 gcc_unreachable ();
18021 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
18022
18023 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18024 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18025
18026 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18027 determine_visibility (TYPE_NAME (type));
18028 /* Now that we know visibility, instantiate the type so we have a
18029 declaration of the op() for later calls to lambda_function. */
18030 complete_type (type);
18031
18032 if (tree fn = lambda_function (type))
18033 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
18034
18035 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18036
18037 insert_pending_capture_proxies ();
18038
18039 RETURN (build_lambda_object (r));
18040 }
18041
18042 case TARGET_EXPR:
18043 /* We can get here for a constant initializer of non-dependent type.
18044 FIXME stop folding in cp_parser_initializer_clause. */
18045 {
18046 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18047 complain);
18048 RETURN (r);
18049 }
18050
18051 case TRANSACTION_EXPR:
18052 RETURN (tsubst_expr(t, args, complain, in_decl,
18053 integral_constant_expression_p));
18054
18055 case PAREN_EXPR:
18056 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18057
18058 case VEC_PERM_EXPR:
18059 {
18060 tree op0 = RECUR (TREE_OPERAND (t, 0));
18061 tree op1 = RECUR (TREE_OPERAND (t, 1));
18062 tree op2 = RECUR (TREE_OPERAND (t, 2));
18063 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18064 complain));
18065 }
18066
18067 case REQUIRES_EXPR:
18068 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18069
18070 default:
18071 /* Handle Objective-C++ constructs, if appropriate. */
18072 {
18073 tree subst
18074 = objcp_tsubst_copy_and_build (t, args, complain,
18075 in_decl, /*function_p=*/false);
18076 if (subst)
18077 RETURN (subst);
18078 }
18079 RETURN (tsubst_copy (t, args, complain, in_decl));
18080 }
18081
18082#undef RECUR
18083#undef RETURN
18084 out:
18085 input_location = loc;
18086 return retval;
18087}
18088
18089/* Verify that the instantiated ARGS are valid. For type arguments,
18090 make sure that the type's linkage is ok. For non-type arguments,
18091 make sure they are constants if they are integral or enumerations.
18092 Emit an error under control of COMPLAIN, and return TRUE on error. */
18093
18094static bool
18095check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18096{
18097 if (dependent_template_arg_p (t))
18098 return false;
18099 if (ARGUMENT_PACK_P (t))
18100 {
18101 tree vec = ARGUMENT_PACK_ARGS (t);
18102 int len = TREE_VEC_LENGTH (vec);
18103 bool result = false;
18104 int i;
18105
18106 for (i = 0; i < len; ++i)
18107 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18108 result = true;
18109 return result;
18110 }
18111 else if (TYPE_P (t))
18112 {
18113 /* [basic.link]: A name with no linkage (notably, the name
18114 of a class or enumeration declared in a local scope)
18115 shall not be used to declare an entity with linkage.
18116 This implies that names with no linkage cannot be used as
18117 template arguments
18118
18119 DR 757 relaxes this restriction for C++0x. */
18120 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18121 : no_linkage_check (t, /*relaxed_p=*/false));
18122
18123 if (nt)
18124 {
18125 /* DR 488 makes use of a type with no linkage cause
18126 type deduction to fail. */
18127 if (complain & tf_error)
18128 {
18129 if (TYPE_UNNAMED_P (nt))
18130 error ("%qT is/uses unnamed type", t);
18131 else
18132 error ("template argument for %qD uses local type %qT",
18133 tmpl, t);
18134 }
18135 return true;
18136 }
18137 /* In order to avoid all sorts of complications, we do not
18138 allow variably-modified types as template arguments. */
18139 else if (variably_modified_type_p (t, NULL_TREE))
18140 {
18141 if (complain & tf_error)
18142 error ("%qT is a variably modified type", t);
18143 return true;
18144 }
18145 }
18146 /* Class template and alias template arguments should be OK. */
18147 else if (DECL_TYPE_TEMPLATE_P (t))
18148 ;
18149 /* A non-type argument of integral or enumerated type must be a
18150 constant. */
18151 else if (TREE_TYPE (t)
18152 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18153 && !REFERENCE_REF_P (t)
18154 && !TREE_CONSTANT (t))
18155 {
18156 if (complain & tf_error)
18157 error ("integral expression %qE is not constant", t);
18158 return true;
18159 }
18160 return false;
18161}
18162
18163static bool
18164check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18165{
18166 int ix, len = DECL_NTPARMS (tmpl);
18167 bool result = false;
18168
18169 for (ix = 0; ix != len; ix++)
18170 {
18171 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18172 result = true;
18173 }
18174 if (result && (complain & tf_error))
18175 error (" trying to instantiate %qD", tmpl);
18176 return result;
18177}
18178
18179/* We're out of SFINAE context now, so generate diagnostics for the access
18180 errors we saw earlier when instantiating D from TMPL and ARGS. */
18181
18182static void
18183recheck_decl_substitution (tree d, tree tmpl, tree args)
18184{
18185 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18186 tree type = TREE_TYPE (pattern);
18187 location_t loc = input_location;
18188
18189 push_access_scope (d);
18190 push_deferring_access_checks (dk_no_deferred);
18191 input_location = DECL_SOURCE_LOCATION (pattern);
18192 tsubst (type, args, tf_warning_or_error, d);
18193 input_location = loc;
18194 pop_deferring_access_checks ();
18195 pop_access_scope (d);
18196}
18197
18198/* Instantiate the indicated variable, function, or alias template TMPL with
18199 the template arguments in TARG_PTR. */
18200
18201static tree
18202instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18203{
18204 tree targ_ptr = orig_args;
18205 tree fndecl;
18206 tree gen_tmpl;
18207 tree spec;
18208 bool access_ok = true;
18209
18210 if (tmpl == error_mark_node)
18211 return error_mark_node;
18212
18213 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18214
18215 /* If this function is a clone, handle it specially. */
18216 if (DECL_CLONED_FUNCTION_P (tmpl))
18217 {
18218 tree spec;
18219 tree clone;
18220
18221 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18222 DECL_CLONED_FUNCTION. */
18223 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18224 targ_ptr, complain);
18225 if (spec == error_mark_node)
18226 return error_mark_node;
18227
18228 /* Look for the clone. */
18229 FOR_EACH_CLONE (clone, spec)
18230 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18231 return clone;
18232 /* We should always have found the clone by now. */
18233 gcc_unreachable ();
18234 return NULL_TREE;
18235 }
18236
18237 if (targ_ptr == error_mark_node)
18238 return error_mark_node;
18239
18240 /* Check to see if we already have this specialization. */
18241 gen_tmpl = most_general_template (tmpl);
18242 if (TMPL_ARGS_DEPTH (targ_ptr)
18243 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18244 /* targ_ptr only has the innermost template args, so add the outer ones
18245 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18246 the case of a non-dependent call within a template definition). */
18247 targ_ptr = (add_outermost_template_args
18248 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18249 targ_ptr));
18250
18251 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18252 but it doesn't seem to be on the hot path. */
18253 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18254
18255 gcc_assert (tmpl == gen_tmpl
18256 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18257 == spec)
18258 || fndecl == NULL_TREE);
18259
18260 if (spec != NULL_TREE)
18261 {
18262 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18263 {
18264 if (complain & tf_error)
18265 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18266 return error_mark_node;
18267 }
18268 return spec;
18269 }
18270
18271 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18272 complain))
18273 return error_mark_node;
18274
18275 /* We are building a FUNCTION_DECL, during which the access of its
18276 parameters and return types have to be checked. However this
18277 FUNCTION_DECL which is the desired context for access checking
18278 is not built yet. We solve this chicken-and-egg problem by
18279 deferring all checks until we have the FUNCTION_DECL. */
18280 push_deferring_access_checks (dk_deferred);
18281
18282 /* Instantiation of the function happens in the context of the function
18283 template, not the context of the overload resolution we're doing. */
18284 push_to_top_level ();
18285 /* If there are dependent arguments, e.g. because we're doing partial
18286 ordering, make sure processing_template_decl stays set. */
18287 if (uses_template_parms (targ_ptr))
18288 ++processing_template_decl;
18289 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18290 {
18291 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18292 complain, gen_tmpl, true);
18293 push_nested_class (ctx);
18294 }
18295
18296 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18297
18298 fndecl = NULL_TREE;
18299 if (VAR_P (pattern))
18300 {
18301 /* We need to determine if we're using a partial or explicit
18302 specialization now, because the type of the variable could be
18303 different. */
18304 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18305 tree elt = most_specialized_partial_spec (tid, complain);
18306 if (elt == error_mark_node)
18307 pattern = error_mark_node;
18308 else if (elt)
18309 {
18310 tree partial_tmpl = TREE_VALUE (elt);
18311 tree partial_args = TREE_PURPOSE (elt);
18312 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18313 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18314 }
18315 }
18316
18317 /* Substitute template parameters to obtain the specialization. */
18318 if (fndecl == NULL_TREE)
18319 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18320 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18321 pop_nested_class ();
18322 pop_from_top_level ();
18323
18324 if (fndecl == error_mark_node)
18325 {
18326 pop_deferring_access_checks ();
18327 return error_mark_node;
18328 }
18329
18330 /* The DECL_TI_TEMPLATE should always be the immediate parent
18331 template, not the most general template. */
18332 DECL_TI_TEMPLATE (fndecl) = tmpl;
18333 DECL_TI_ARGS (fndecl) = targ_ptr;
18334
18335 /* Now we know the specialization, compute access previously
18336 deferred. Do no access control for inheriting constructors,
18337 as we already checked access for the inherited constructor. */
18338 if (!(flag_new_inheriting_ctors
18339 && DECL_INHERITED_CTOR (fndecl)))
18340 {
18341 push_access_scope (fndecl);
18342 if (!perform_deferred_access_checks (complain))
18343 access_ok = false;
18344 pop_access_scope (fndecl);
18345 }
18346 pop_deferring_access_checks ();
18347
18348 /* If we've just instantiated the main entry point for a function,
18349 instantiate all the alternate entry points as well. We do this
18350 by cloning the instantiation of the main entry point, not by
18351 instantiating the template clones. */
18352 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18353 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18354
18355 if (!access_ok)
18356 {
18357 if (!(complain & tf_error))
18358 {
18359 /* Remember to reinstantiate when we're out of SFINAE so the user
18360 can see the errors. */
18361 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18362 }
18363 return error_mark_node;
18364 }
18365 return fndecl;
18366}
18367
18368/* Wrapper for instantiate_template_1. */
18369
18370tree
18371instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18372{
18373 tree ret;
18374 timevar_push (TV_TEMPLATE_INST);
18375 ret = instantiate_template_1 (tmpl, orig_args, complain);
18376 timevar_pop (TV_TEMPLATE_INST);
18377 return ret;
18378}
18379
18380/* Instantiate the alias template TMPL with ARGS. Also push a template
18381 instantiation level, which instantiate_template doesn't do because
18382 functions and variables have sufficient context established by the
18383 callers. */
18384
18385static tree
18386instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18387{
18388 struct pending_template *old_last_pend = last_pending_template;
18389 struct tinst_level *old_error_tinst = last_error_tinst_level;
18390 if (tmpl == error_mark_node || args == error_mark_node)
18391 return error_mark_node;
18392 tree tinst = build_tree_list (tmpl, args);
18393 if (!push_tinst_level (tinst))
18394 {
18395 ggc_free (tinst);
18396 return error_mark_node;
18397 }
18398
18399 args =
18400 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18401 args, tmpl, complain,
18402 /*require_all_args=*/true,
18403 /*use_default_args=*/true);
18404
18405 tree r = instantiate_template (tmpl, args, complain);
18406 pop_tinst_level ();
18407 /* We can't free this if a pending_template entry or last_error_tinst_level
18408 is pointing at it. */
18409 if (last_pending_template == old_last_pend
18410 && last_error_tinst_level == old_error_tinst)
18411 ggc_free (tinst);
18412
18413 return r;
18414}
18415
18416/* PARM is a template parameter pack for FN. Returns true iff
18417 PARM is used in a deducible way in the argument list of FN. */
18418
18419static bool
18420pack_deducible_p (tree parm, tree fn)
18421{
18422 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18423 for (; t; t = TREE_CHAIN (t))
18424 {
18425 tree type = TREE_VALUE (t);
18426 tree packs;
18427 if (!PACK_EXPANSION_P (type))
18428 continue;
18429 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18430 packs; packs = TREE_CHAIN (packs))
18431 if (template_args_equal (TREE_VALUE (packs), parm))
18432 {
18433 /* The template parameter pack is used in a function parameter
18434 pack. If this is the end of the parameter list, the
18435 template parameter pack is deducible. */
18436 if (TREE_CHAIN (t) == void_list_node)
18437 return true;
18438 else
18439 /* Otherwise, not. Well, it could be deduced from
18440 a non-pack parameter, but doing so would end up with
18441 a deduction mismatch, so don't bother. */
18442 return false;
18443 }
18444 }
18445 /* The template parameter pack isn't used in any function parameter
18446 packs, but it might be used deeper, e.g. tuple<Args...>. */
18447 return true;
18448}
18449
18450/* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18451 NARGS elements of the arguments that are being used when calling
18452 it. TARGS is a vector into which the deduced template arguments
18453 are placed.
18454
18455 Returns either a FUNCTION_DECL for the matching specialization of FN or
18456 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18457 true, diagnostics will be printed to explain why it failed.
18458
18459 If FN is a conversion operator, or we are trying to produce a specific
18460 specialization, RETURN_TYPE is the return type desired.
18461
18462 The EXPLICIT_TARGS are explicit template arguments provided via a
18463 template-id.
18464
18465 The parameter STRICT is one of:
18466
18467 DEDUCE_CALL:
18468 We are deducing arguments for a function call, as in
18469 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18470 deducing arguments for a call to the result of a conversion
18471 function template, as in [over.call.object].
18472
18473 DEDUCE_CONV:
18474 We are deducing arguments for a conversion function, as in
18475 [temp.deduct.conv].
18476
18477 DEDUCE_EXACT:
18478 We are deducing arguments when doing an explicit instantiation
18479 as in [temp.explicit], when determining an explicit specialization
18480 as in [temp.expl.spec], or when taking the address of a function
18481 template, as in [temp.deduct.funcaddr]. */
18482
18483tree
18484fn_type_unification (tree fn,
18485 tree explicit_targs,
18486 tree targs,
18487 const tree *args,
18488 unsigned int nargs,
18489 tree return_type,
18490 unification_kind_t strict,
18491 int flags,
18492 bool explain_p,
18493 bool decltype_p)
18494{
18495 tree parms;
18496 tree fntype;
18497 tree decl = NULL_TREE;
18498 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18499 bool ok;
18500 static int deduction_depth;
18501 struct pending_template *old_last_pend = last_pending_template;
18502 struct tinst_level *old_error_tinst = last_error_tinst_level;
18503
18504 tree orig_fn = fn;
18505 if (flag_new_inheriting_ctors)
18506 fn = strip_inheriting_ctors (fn);
18507
18508 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18509 tree tinst;
18510 tree r = error_mark_node;
18511
18512 tree full_targs = targs;
18513 if (TMPL_ARGS_DEPTH (targs)
18514 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18515 full_targs = (add_outermost_template_args
18516 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18517 targs));
18518
18519 if (decltype_p)
18520 complain |= tf_decltype;
18521
18522 /* In C++0x, it's possible to have a function template whose type depends
18523 on itself recursively. This is most obvious with decltype, but can also
18524 occur with enumeration scope (c++/48969). So we need to catch infinite
18525 recursion and reject the substitution at deduction time; this function
18526 will return error_mark_node for any repeated substitution.
18527
18528 This also catches excessive recursion such as when f<N> depends on
18529 f<N-1> across all integers, and returns error_mark_node for all the
18530 substitutions back up to the initial one.
18531
18532 This is, of course, not reentrant. */
18533 if (excessive_deduction_depth)
18534 return error_mark_node;
18535 tinst = build_tree_list (fn, NULL_TREE);
18536 ++deduction_depth;
18537
18538 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18539
18540 fntype = TREE_TYPE (fn);
18541 if (explicit_targs)
18542 {
18543 /* [temp.deduct]
18544
18545 The specified template arguments must match the template
18546 parameters in kind (i.e., type, nontype, template), and there
18547 must not be more arguments than there are parameters;
18548 otherwise type deduction fails.
18549
18550 Nontype arguments must match the types of the corresponding
18551 nontype template parameters, or must be convertible to the
18552 types of the corresponding nontype parameters as specified in
18553 _temp.arg.nontype_, otherwise type deduction fails.
18554
18555 All references in the function type of the function template
18556 to the corresponding template parameters are replaced by the
18557 specified template argument values. If a substitution in a
18558 template parameter or in the function type of the function
18559 template results in an invalid type, type deduction fails. */
18560 int i, len = TREE_VEC_LENGTH (tparms);
18561 location_t loc = input_location;
18562 bool incomplete = false;
18563
18564 if (explicit_targs == error_mark_node)
18565 goto fail;
18566
18567 if (TMPL_ARGS_DEPTH (explicit_targs)
18568 < TMPL_ARGS_DEPTH (full_targs))
18569 explicit_targs = add_outermost_template_args (full_targs,
18570 explicit_targs);
18571
18572 /* Adjust any explicit template arguments before entering the
18573 substitution context. */
18574 explicit_targs
18575 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18576 complain,
18577 /*require_all_args=*/false,
18578 /*use_default_args=*/false));
18579 if (explicit_targs == error_mark_node)
18580 goto fail;
18581
18582 /* Substitute the explicit args into the function type. This is
18583 necessary so that, for instance, explicitly declared function
18584 arguments can match null pointed constants. If we were given
18585 an incomplete set of explicit args, we must not do semantic
18586 processing during substitution as we could create partial
18587 instantiations. */
18588 for (i = 0; i < len; i++)
18589 {
18590 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18591 bool parameter_pack = false;
18592 tree targ = TREE_VEC_ELT (explicit_targs, i);
18593
18594 /* Dig out the actual parm. */
18595 if (TREE_CODE (parm) == TYPE_DECL
18596 || TREE_CODE (parm) == TEMPLATE_DECL)
18597 {
18598 parm = TREE_TYPE (parm);
18599 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18600 }
18601 else if (TREE_CODE (parm) == PARM_DECL)
18602 {
18603 parm = DECL_INITIAL (parm);
18604 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18605 }
18606
18607 if (!parameter_pack && targ == NULL_TREE)
18608 /* No explicit argument for this template parameter. */
18609 incomplete = true;
18610
18611 if (parameter_pack && pack_deducible_p (parm, fn))
18612 {
18613 /* Mark the argument pack as "incomplete". We could
18614 still deduce more arguments during unification.
18615 We remove this mark in type_unification_real. */
18616 if (targ)
18617 {
18618 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18619 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18620 = ARGUMENT_PACK_ARGS (targ);
18621 }
18622
18623 /* We have some incomplete argument packs. */
18624 incomplete = true;
18625 }
18626 }
18627
18628 TREE_VALUE (tinst) = explicit_targs;
18629 if (!push_tinst_level (tinst))
18630 {
18631 excessive_deduction_depth = true;
18632 goto fail;
18633 }
18634 processing_template_decl += incomplete;
18635 input_location = DECL_SOURCE_LOCATION (fn);
18636 /* Ignore any access checks; we'll see them again in
18637 instantiate_template and they might have the wrong
18638 access path at this point. */
18639 push_deferring_access_checks (dk_deferred);
18640 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18641 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18642 pop_deferring_access_checks ();
18643 input_location = loc;
18644 processing_template_decl -= incomplete;
18645 pop_tinst_level ();
18646
18647 if (fntype == error_mark_node)
18648 goto fail;
18649
18650 /* Place the explicitly specified arguments in TARGS. */
18651 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18652 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18653 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18654 }
18655
18656 /* Never do unification on the 'this' parameter. */
18657 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18658
18659 if (return_type && strict == DEDUCE_CALL)
18660 {
18661 /* We're deducing for a call to the result of a template conversion
18662 function. The parms we really want are in return_type. */
18663 if (POINTER_TYPE_P (return_type))
18664 return_type = TREE_TYPE (return_type);
18665 parms = TYPE_ARG_TYPES (return_type);
18666 }
18667 else if (return_type)
18668 {
18669 tree *new_args;
18670
18671 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18672 new_args = XALLOCAVEC (tree, nargs + 1);
18673 new_args[0] = return_type;
18674 memcpy (new_args + 1, args, nargs * sizeof (tree));
18675 args = new_args;
18676 ++nargs;
18677 }
18678
18679 /* We allow incomplete unification without an error message here
18680 because the standard doesn't seem to explicitly prohibit it. Our
18681 callers must be ready to deal with unification failures in any
18682 event. */
18683
18684 TREE_VALUE (tinst) = targs;
18685 /* If we aren't explaining yet, push tinst context so we can see where
18686 any errors (e.g. from class instantiations triggered by instantiation
18687 of default template arguments) come from. If we are explaining, this
18688 context is redundant. */
18689 if (!explain_p && !push_tinst_level (tinst))
18690 {
18691 excessive_deduction_depth = true;
18692 goto fail;
18693 }
18694
18695 /* type_unification_real will pass back any access checks from default
18696 template argument substitution. */
18697 vec<deferred_access_check, va_gc> *checks;
18698 checks = NULL;
18699
18700 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18701 full_targs, parms, args, nargs, /*subr=*/0,
18702 strict, flags, &checks, explain_p);
18703 if (!explain_p)
18704 pop_tinst_level ();
18705 if (!ok)
18706 goto fail;
18707
18708 /* Now that we have bindings for all of the template arguments,
18709 ensure that the arguments deduced for the template template
18710 parameters have compatible template parameter lists. We cannot
18711 check this property before we have deduced all template
18712 arguments, because the template parameter types of a template
18713 template parameter might depend on prior template parameters
18714 deduced after the template template parameter. The following
18715 ill-formed example illustrates this issue:
18716
18717 template<typename T, template<T> class C> void f(C<5>, T);
18718
18719 template<int N> struct X {};
18720
18721 void g() {
18722 f(X<5>(), 5l); // error: template argument deduction fails
18723 }
18724
18725 The template parameter list of 'C' depends on the template type
18726 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18727 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18728 time that we deduce 'C'. */
18729 if (!template_template_parm_bindings_ok_p
18730 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18731 {
18732 unify_inconsistent_template_template_parameters (explain_p);
18733 goto fail;
18734 }
18735
18736 /* All is well so far. Now, check:
18737
18738 [temp.deduct]
18739
18740 When all template arguments have been deduced, all uses of
18741 template parameters in nondeduced contexts are replaced with
18742 the corresponding deduced argument values. If the
18743 substitution results in an invalid type, as described above,
18744 type deduction fails. */
18745 TREE_VALUE (tinst) = targs;
18746 if (!push_tinst_level (tinst))
18747 {
18748 excessive_deduction_depth = true;
18749 goto fail;
18750 }
18751
18752 /* Also collect access checks from the instantiation. */
18753 reopen_deferring_access_checks (checks);
18754
18755 decl = instantiate_template (fn, targs, complain);
18756
18757 checks = get_deferred_access_checks ();
18758 pop_deferring_access_checks ();
18759
18760 pop_tinst_level ();
18761
18762 if (decl == error_mark_node)
18763 goto fail;
18764
18765 /* Now perform any access checks encountered during substitution. */
18766 push_access_scope (decl);
18767 ok = perform_access_checks (checks, complain);
18768 pop_access_scope (decl);
18769 if (!ok)
18770 goto fail;
18771
18772 /* If we're looking for an exact match, check that what we got
18773 is indeed an exact match. It might not be if some template
18774 parameters are used in non-deduced contexts. But don't check
18775 for an exact match if we have dependent template arguments;
18776 in that case we're doing partial ordering, and we already know
18777 that we have two candidates that will provide the actual type. */
18778 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18779 {
18780 tree substed = TREE_TYPE (decl);
18781 unsigned int i;
18782
18783 tree sarg
18784 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18785 if (return_type)
18786 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18787 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18788 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18789 {
18790 unify_type_mismatch (explain_p, args[i],
18791 TREE_VALUE (sarg));
18792 goto fail;
18793 }
18794 }
18795
18796 /* After doing deduction with the inherited constructor, actually return an
18797 instantiation of the inheriting constructor. */
18798 if (orig_fn != fn)
18799 decl = instantiate_template (orig_fn, targs, complain);
18800
18801 r = decl;
18802
18803 fail:
18804 --deduction_depth;
18805 if (excessive_deduction_depth)
18806 {
18807 if (deduction_depth == 0)
18808 /* Reset once we're all the way out. */
18809 excessive_deduction_depth = false;
18810 }
18811
18812 /* We can't free this if a pending_template entry or last_error_tinst_level
18813 is pointing at it. */
18814 if (last_pending_template == old_last_pend
18815 && last_error_tinst_level == old_error_tinst)
18816 ggc_free (tinst);
18817
18818 return r;
18819}
18820
18821/* Adjust types before performing type deduction, as described in
18822 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18823 sections are symmetric. PARM is the type of a function parameter
18824 or the return type of the conversion function. ARG is the type of
18825 the argument passed to the call, or the type of the value
18826 initialized with the result of the conversion function.
18827 ARG_EXPR is the original argument expression, which may be null. */
18828
18829static int
18830maybe_adjust_types_for_deduction (unification_kind_t strict,
18831 tree* parm,
18832 tree* arg,
18833 tree arg_expr)
18834{
18835 int result = 0;
18836
18837 switch (strict)
18838 {
18839 case DEDUCE_CALL:
18840 break;
18841
18842 case DEDUCE_CONV:
18843 /* Swap PARM and ARG throughout the remainder of this
18844 function; the handling is precisely symmetric since PARM
18845 will initialize ARG rather than vice versa. */
18846 std::swap (parm, arg);
18847 break;
18848
18849 case DEDUCE_EXACT:
18850 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18851 too, but here handle it by stripping the reference from PARM
18852 rather than by adding it to ARG. */
18853 if (TREE_CODE (*parm) == REFERENCE_TYPE
18854 && TYPE_REF_IS_RVALUE (*parm)
18855 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18856 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18857 && TREE_CODE (*arg) == REFERENCE_TYPE
18858 && !TYPE_REF_IS_RVALUE (*arg))
18859 *parm = TREE_TYPE (*parm);
18860 /* Nothing else to do in this case. */
18861 return 0;
18862
18863 default:
18864 gcc_unreachable ();
18865 }
18866
18867 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18868 {
18869 /* [temp.deduct.call]
18870
18871 If P is not a reference type:
18872
18873 --If A is an array type, the pointer type produced by the
18874 array-to-pointer standard conversion (_conv.array_) is
18875 used in place of A for type deduction; otherwise,
18876
18877 --If A is a function type, the pointer type produced by
18878 the function-to-pointer standard conversion
18879 (_conv.func_) is used in place of A for type deduction;
18880 otherwise,
18881
18882 --If A is a cv-qualified type, the top level
18883 cv-qualifiers of A's type are ignored for type
18884 deduction. */
18885 if (TREE_CODE (*arg) == ARRAY_TYPE)
18886 *arg = build_pointer_type (TREE_TYPE (*arg));
18887 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18888 *arg = build_pointer_type (*arg);
18889 else
18890 *arg = TYPE_MAIN_VARIANT (*arg);
18891 }
18892
18893 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18894 reference to a cv-unqualified template parameter that does not represent a
18895 template parameter of a class template (during class template argument
18896 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18897 an lvalue, the type "lvalue reference to A" is used in place of A for type
18898 deduction. */
18899 if (TREE_CODE (*parm) == REFERENCE_TYPE
18900 && TYPE_REF_IS_RVALUE (*parm)
18901 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18902 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18903 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18904 && (arg_expr ? lvalue_p (arg_expr)
18905 /* try_one_overload doesn't provide an arg_expr, but
18906 functions are always lvalues. */
18907 : TREE_CODE (*arg) == FUNCTION_TYPE))
18908 *arg = build_reference_type (*arg);
18909
18910 /* [temp.deduct.call]
18911
18912 If P is a cv-qualified type, the top level cv-qualifiers
18913 of P's type are ignored for type deduction. If P is a
18914 reference type, the type referred to by P is used for
18915 type deduction. */
18916 *parm = TYPE_MAIN_VARIANT (*parm);
18917 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18918 {
18919 *parm = TREE_TYPE (*parm);
18920 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18921 }
18922
18923 /* DR 322. For conversion deduction, remove a reference type on parm
18924 too (which has been swapped into ARG). */
18925 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18926 *arg = TREE_TYPE (*arg);
18927
18928 return result;
18929}
18930
18931/* Subroutine of unify_one_argument. PARM is a function parameter of a
18932 template which does contain any deducible template parameters; check if
18933 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18934 unify_one_argument. */
18935
18936static int
18937check_non_deducible_conversion (tree parm, tree arg, int strict,
18938 int flags, bool explain_p)
18939{
18940 tree type;
18941
18942 if (!TYPE_P (arg))
18943 type = TREE_TYPE (arg);
18944 else
18945 type = arg;
18946
18947 if (same_type_p (parm, type))
18948 return unify_success (explain_p);
18949
18950 if (strict == DEDUCE_CONV)
18951 {
18952 if (can_convert_arg (type, parm, NULL_TREE, flags,
18953 explain_p ? tf_warning_or_error : tf_none))
18954 return unify_success (explain_p);
18955 }
18956 else if (strict != DEDUCE_EXACT)
18957 {
18958 if (can_convert_arg (parm, type,
18959 TYPE_P (arg) ? NULL_TREE : arg,
18960 flags, explain_p ? tf_warning_or_error : tf_none))
18961 return unify_success (explain_p);
18962 }
18963
18964 if (strict == DEDUCE_EXACT)
18965 return unify_type_mismatch (explain_p, parm, arg);
18966 else
18967 return unify_arg_conversion (explain_p, parm, type, arg);
18968}
18969
18970static bool uses_deducible_template_parms (tree type);
18971
18972/* Returns true iff the expression EXPR is one from which a template
18973 argument can be deduced. In other words, if it's an undecorated
18974 use of a template non-type parameter. */
18975
18976static bool
18977deducible_expression (tree expr)
18978{
18979 /* Strip implicit conversions. */
18980 while (CONVERT_EXPR_P (expr))
18981 expr = TREE_OPERAND (expr, 0);
18982 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18983}
18984
18985/* Returns true iff the array domain DOMAIN uses a template parameter in a
18986 deducible way; that is, if it has a max value of <PARM> - 1. */
18987
18988static bool
18989deducible_array_bound (tree domain)
18990{
18991 if (domain == NULL_TREE)
18992 return false;
18993
18994 tree max = TYPE_MAX_VALUE (domain);
18995 if (TREE_CODE (max) != MINUS_EXPR)
18996 return false;
18997
18998 return deducible_expression (TREE_OPERAND (max, 0));
18999}
19000
19001/* Returns true iff the template arguments ARGS use a template parameter
19002 in a deducible way. */
19003
19004static bool
19005deducible_template_args (tree args)
19006{
19007 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19008 {
19009 bool deducible;
19010 tree elt = TREE_VEC_ELT (args, i);
19011 if (ARGUMENT_PACK_P (elt))
19012 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19013 else
19014 {
19015 if (PACK_EXPANSION_P (elt))
19016 elt = PACK_EXPANSION_PATTERN (elt);
19017 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19018 deducible = true;
19019 else if (TYPE_P (elt))
19020 deducible = uses_deducible_template_parms (elt);
19021 else
19022 deducible = deducible_expression (elt);
19023 }
19024 if (deducible)
19025 return true;
19026 }
19027 return false;
19028}
19029
19030/* Returns true iff TYPE contains any deducible references to template
19031 parameters, as per 14.8.2.5. */
19032
19033static bool
19034uses_deducible_template_parms (tree type)
19035{
19036 if (PACK_EXPANSION_P (type))
19037 type = PACK_EXPANSION_PATTERN (type);
19038
19039 /* T
19040 cv-list T
19041 TT<T>
19042 TT<i>
19043 TT<> */
19044 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19045 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19046 return true;
19047
19048 /* T*
19049 T&
19050 T&& */
19051 if (POINTER_TYPE_P (type))
19052 return uses_deducible_template_parms (TREE_TYPE (type));
19053
19054 /* T[integer-constant ]
19055 type [i] */
19056 if (TREE_CODE (type) == ARRAY_TYPE)
19057 return (uses_deducible_template_parms (TREE_TYPE (type))
19058 || deducible_array_bound (TYPE_DOMAIN (type)));
19059
19060 /* T type ::*
19061 type T::*
19062 T T::*
19063 T (type ::*)()
19064 type (T::*)()
19065 type (type ::*)(T)
19066 type (T::*)(T)
19067 T (type ::*)(T)
19068 T (T::*)()
19069 T (T::*)(T) */
19070 if (TYPE_PTRMEM_P (type))
19071 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19072 || (uses_deducible_template_parms
19073 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19074
19075 /* template-name <T> (where template-name refers to a class template)
19076 template-name <i> (where template-name refers to a class template) */
19077 if (CLASS_TYPE_P (type)
19078 && CLASSTYPE_TEMPLATE_INFO (type)
19079 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19080 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19081 (CLASSTYPE_TI_ARGS (type)));
19082
19083 /* type (T)
19084 T()
19085 T(T) */
19086 if (TREE_CODE (type) == FUNCTION_TYPE
19087 || TREE_CODE (type) == METHOD_TYPE)
19088 {
19089 if (uses_deducible_template_parms (TREE_TYPE (type)))
19090 return true;
19091 tree parm = TYPE_ARG_TYPES (type);
19092 if (TREE_CODE (type) == METHOD_TYPE)
19093 parm = TREE_CHAIN (parm);
19094 for (; parm; parm = TREE_CHAIN (parm))
19095 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19096 return true;
19097 }
19098
19099 return false;
19100}
19101
19102/* Subroutine of type_unification_real and unify_pack_expansion to
19103 handle unification of a single P/A pair. Parameters are as
19104 for those functions. */
19105
19106static int
19107unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19108 int subr, unification_kind_t strict,
19109 bool explain_p)
19110{
19111 tree arg_expr = NULL_TREE;
19112 int arg_strict;
19113
19114 if (arg == error_mark_node || parm == error_mark_node)
19115 return unify_invalid (explain_p);
19116 if (arg == unknown_type_node)
19117 /* We can't deduce anything from this, but we might get all the
19118 template args from other function args. */
19119 return unify_success (explain_p);
19120
19121 /* Implicit conversions (Clause 4) will be performed on a function
19122 argument to convert it to the type of the corresponding function
19123 parameter if the parameter type contains no template-parameters that
19124 participate in template argument deduction. */
19125 if (strict != DEDUCE_EXACT
19126 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19127 /* For function parameters with no deducible template parameters,
19128 just return. We'll check non-dependent conversions later. */
19129 return unify_success (explain_p);
19130
19131 switch (strict)
19132 {
19133 case DEDUCE_CALL:
19134 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19135 | UNIFY_ALLOW_MORE_CV_QUAL
19136 | UNIFY_ALLOW_DERIVED);
19137 break;
19138
19139 case DEDUCE_CONV:
19140 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19141 break;
19142
19143 case DEDUCE_EXACT:
19144 arg_strict = UNIFY_ALLOW_NONE;
19145 break;
19146
19147 default:
19148 gcc_unreachable ();
19149 }
19150
19151 /* We only do these transformations if this is the top-level
19152 parameter_type_list in a call or declaration matching; in other
19153 situations (nested function declarators, template argument lists) we
19154 won't be comparing a type to an expression, and we don't do any type
19155 adjustments. */
19156 if (!subr)
19157 {
19158 if (!TYPE_P (arg))
19159 {
19160 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19161 if (type_unknown_p (arg))
19162 {
19163 /* [temp.deduct.type] A template-argument can be
19164 deduced from a pointer to function or pointer
19165 to member function argument if the set of
19166 overloaded functions does not contain function
19167 templates and at most one of a set of
19168 overloaded functions provides a unique
19169 match. */
19170
19171 if (resolve_overloaded_unification
19172 (tparms, targs, parm, arg, strict,
19173 arg_strict, explain_p))
19174 return unify_success (explain_p);
19175 return unify_overload_resolution_failure (explain_p, arg);
19176 }
19177
19178 arg_expr = arg;
19179 arg = unlowered_expr_type (arg);
19180 if (arg == error_mark_node)
19181 return unify_invalid (explain_p);
19182 }
19183
19184 arg_strict |=
19185 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19186 }
19187 else
19188 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19189 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19190 return unify_template_argument_mismatch (explain_p, parm, arg);
19191
19192 /* For deduction from an init-list we need the actual list. */
19193 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19194 arg = arg_expr;
19195 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19196}
19197
19198/* for_each_template_parm callback that always returns 0. */
19199
19200static int
19201zero_r (tree, void *)
19202{
19203 return 0;
19204}
19205
19206/* for_each_template_parm any_fn callback to handle deduction of a template
19207 type argument from the type of an array bound. */
19208
19209static int
19210array_deduction_r (tree t, void *data)
19211{
19212 tree_pair_p d = (tree_pair_p)data;
19213 tree &tparms = d->purpose;
19214 tree &targs = d->value;
19215
19216 if (TREE_CODE (t) == ARRAY_TYPE)
19217 if (tree dom = TYPE_DOMAIN (t))
19218 if (tree max = TYPE_MAX_VALUE (dom))
19219 {
19220 if (TREE_CODE (max) == MINUS_EXPR)
19221 max = TREE_OPERAND (max, 0);
19222 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19223 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19224 UNIFY_ALLOW_NONE, /*explain*/false);
19225 }
19226
19227 /* Keep walking. */
19228 return 0;
19229}
19230
19231/* Try to deduce any not-yet-deduced template type arguments from the type of
19232 an array bound. This is handled separately from unify because 14.8.2.5 says
19233 "The type of a type parameter is only deduced from an array bound if it is
19234 not otherwise deduced." */
19235
19236static void
19237try_array_deduction (tree tparms, tree targs, tree parm)
19238{
19239 tree_pair_s data = { tparms, targs };
19240 hash_set<tree> visited;
19241 for_each_template_parm (parm, zero_r, &data, &visited,
19242 /*nondeduced*/false, array_deduction_r);
19243}
19244
19245/* Returns how many levels of { } INIT contains. */
19246
19247static int
19248braced_init_depth (tree init)
19249{
19250 if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
19251 return 0;
19252 unsigned i; tree val;
19253 unsigned max = 0;
19254 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), i, val)
19255 {
19256 unsigned elt_d = braced_init_depth (val);
19257 if (elt_d > max)
19258 max = elt_d;
19259 }
19260 return max + 1;
19261}
19262
19263/* Most parms like fn_type_unification.
19264
19265 If SUBR is 1, we're being called recursively (to unify the
19266 arguments of a function or method parameter of a function
19267 template).
19268
19269 CHECKS is a pointer to a vector of access checks encountered while
19270 substituting default template arguments. */
19271
19272static int
19273type_unification_real (tree tparms,
19274 tree full_targs,
19275 tree xparms,
19276 const tree *xargs,
19277 unsigned int xnargs,
19278 int subr,
19279 unification_kind_t strict,
19280 int flags,
19281 vec<deferred_access_check, va_gc> **checks,
19282 bool explain_p)
19283{
19284 tree parm, arg;
19285 int i;
19286 int ntparms = TREE_VEC_LENGTH (tparms);
19287 int saw_undeduced = 0;
19288 tree parms;
19289 const tree *args;
19290 unsigned int nargs;
19291 unsigned int ia;
19292
19293 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19294 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19295 gcc_assert (ntparms > 0);
19296
19297 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19298
19299 /* Reset the number of non-defaulted template arguments contained
19300 in TARGS. */
19301 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19302
19303 again:
19304 parms = xparms;
19305 args = xargs;
19306 nargs = xnargs;
19307
19308 ia = 0;
19309 while (parms && parms != void_list_node
19310 && ia < nargs)
19311 {
19312 parm = TREE_VALUE (parms);
19313
19314 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19315 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19316 /* For a function parameter pack that occurs at the end of the
19317 parameter-declaration-list, the type A of each remaining
19318 argument of the call is compared with the type P of the
19319 declarator-id of the function parameter pack. */
19320 break;
19321
19322 parms = TREE_CHAIN (parms);
19323
19324 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19325 /* For a function parameter pack that does not occur at the
19326 end of the parameter-declaration-list, the type of the
19327 parameter pack is a non-deduced context. */
19328 continue;
19329
19330 arg = args[ia];
19331 ++ia;
19332
19333 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19334 explain_p))
19335 return 1;
19336 }
19337
19338 if (parms
19339 && parms != void_list_node
19340 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19341 {
19342 /* Unify the remaining arguments with the pack expansion type. */
19343 tree argvec;
19344 tree parmvec = make_tree_vec (1);
19345
19346 /* Allocate a TREE_VEC and copy in all of the arguments */
19347 argvec = make_tree_vec (nargs - ia);
19348 for (i = 0; ia < nargs; ++ia, ++i)
19349 TREE_VEC_ELT (argvec, i) = args[ia];
19350
19351 /* Copy the parameter into parmvec. */
19352 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19353 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19354 /*subr=*/subr, explain_p))
19355 return 1;
19356
19357 /* Advance to the end of the list of parameters. */
19358 parms = TREE_CHAIN (parms);
19359 }
19360
19361 /* Fail if we've reached the end of the parm list, and more args
19362 are present, and the parm list isn't variadic. */
19363 if (ia < nargs && parms == void_list_node)
19364 return unify_too_many_arguments (explain_p, nargs, ia);
19365 /* Fail if parms are left and they don't have default values and
19366 they aren't all deduced as empty packs (c++/57397). This is
19367 consistent with sufficient_parms_p. */
19368 if (parms && parms != void_list_node
19369 && TREE_PURPOSE (parms) == NULL_TREE)
19370 {
19371 unsigned int count = nargs;
19372 tree p = parms;
19373 bool type_pack_p;
19374 do
19375 {
19376 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19377 if (!type_pack_p)
19378 count++;
19379 p = TREE_CHAIN (p);
19380 }
19381 while (p && p != void_list_node);
19382 if (count != nargs)
19383 return unify_too_few_arguments (explain_p, ia, count,
19384 type_pack_p);
19385 }
19386
19387 if (!subr)
19388 {
19389 tsubst_flags_t complain = (explain_p
19390 ? tf_warning_or_error
19391 : tf_none);
19392 bool tried_array_deduction = (cxx_dialect < cxx1z);
19393
19394 for (i = 0; i < ntparms; i++)
19395 {
19396 tree targ = TREE_VEC_ELT (targs, i);
19397 tree tparm = TREE_VEC_ELT (tparms, i);
19398
19399 /* Clear the "incomplete" flags on all argument packs now so that
19400 substituting them into later default arguments works. */
19401 if (targ && ARGUMENT_PACK_P (targ))
19402 {
19403 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19404 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19405 }
19406
19407 if (targ || tparm == error_mark_node)
19408 continue;
19409 tparm = TREE_VALUE (tparm);
19410
19411 if (TREE_CODE (tparm) == TYPE_DECL
19412 && !tried_array_deduction)
19413 {
19414 try_array_deduction (tparms, targs, xparms);
19415 tried_array_deduction = true;
19416 if (TREE_VEC_ELT (targs, i))
19417 continue;
19418 }
19419
19420 /* If this is an undeduced nontype parameter that depends on
19421 a type parameter, try another pass; its type may have been
19422 deduced from a later argument than the one from which
19423 this parameter can be deduced. */
19424 if (TREE_CODE (tparm) == PARM_DECL
19425 && uses_template_parms (TREE_TYPE (tparm))
19426 && saw_undeduced < 2)
19427 {
19428 saw_undeduced = 1;
19429 continue;
19430 }
19431
19432 /* Core issue #226 (C++0x) [temp.deduct]:
19433
19434 If a template argument has not been deduced, its
19435 default template argument, if any, is used.
19436
19437 When we are in C++98 mode, TREE_PURPOSE will either
19438 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19439 to explicitly check cxx_dialect here. */
19440 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19441 /* OK, there is a default argument. Wait until after the
19442 conversion check to do substitution. */
19443 continue;
19444
19445 /* If the type parameter is a parameter pack, then it will
19446 be deduced to an empty parameter pack. */
19447 if (template_parameter_pack_p (tparm))
19448 {
19449 tree arg;
19450
19451 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19452 {
19453 arg = make_node (NONTYPE_ARGUMENT_PACK);
19454 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19455 TREE_CONSTANT (arg) = 1;
19456 }
19457 else
19458 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19459
19460 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19461
19462 TREE_VEC_ELT (targs, i) = arg;
19463 continue;
19464 }
19465
19466 return unify_parameter_deduction_failure (explain_p, tparm);
19467 }
19468
19469 /* DR 1391: All parameters have args, now check non-dependent parms for
19470 convertibility. */
19471 if (saw_undeduced < 2)
19472 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19473 parms && parms != void_list_node && ia < nargs; )
19474 {
19475 parm = TREE_VALUE (parms);
19476
19477 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19478 && (!TREE_CHAIN (parms)
19479 || TREE_CHAIN (parms) == void_list_node))
19480 /* For a function parameter pack that occurs at the end of the
19481 parameter-declaration-list, the type A of each remaining
19482 argument of the call is compared with the type P of the
19483 declarator-id of the function parameter pack. */
19484 break;
19485
19486 parms = TREE_CHAIN (parms);
19487
19488 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19489 /* For a function parameter pack that does not occur at the
19490 end of the parameter-declaration-list, the type of the
19491 parameter pack is a non-deduced context. */
19492 continue;
19493
19494 arg = args[ia];
19495 ++ia;
19496
19497 if (uses_template_parms (parm))
19498 continue;
19499 /* Workaround for c++/80290: avoid combinatorial explosion on
19500 deeply nested braced init-lists. */
19501 if (braced_init_depth (arg) > 2)
19502 continue;
19503 if (check_non_deducible_conversion (parm, arg, strict, flags,
19504 explain_p))
19505 return 1;
19506 }
19507
19508 /* Now substitute into the default template arguments. */
19509 for (i = 0; i < ntparms; i++)
19510 {
19511 tree targ = TREE_VEC_ELT (targs, i);
19512 tree tparm = TREE_VEC_ELT (tparms, i);
19513
19514 if (targ || tparm == error_mark_node)
19515 continue;
19516 tree parm = TREE_VALUE (tparm);
19517
19518 if (TREE_CODE (parm) == PARM_DECL
19519 && uses_template_parms (TREE_TYPE (parm))
19520 && saw_undeduced < 2)
19521 continue;
19522
19523 tree arg = TREE_PURPOSE (tparm);
19524 reopen_deferring_access_checks (*checks);
19525 location_t save_loc = input_location;
19526 if (DECL_P (parm))
19527 input_location = DECL_SOURCE_LOCATION (parm);
19528 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19529 if (!uses_template_parms (arg))
19530 arg = convert_template_argument (parm, arg, full_targs, complain,
19531 i, NULL_TREE);
19532 else if (saw_undeduced < 2)
19533 arg = NULL_TREE;
19534 else
19535 arg = error_mark_node;
19536 input_location = save_loc;
19537 *checks = get_deferred_access_checks ();
19538 pop_deferring_access_checks ();
19539 if (arg == error_mark_node)
19540 return 1;
19541 else if (arg)
19542 {
19543 TREE_VEC_ELT (targs, i) = arg;
19544 /* The position of the first default template argument,
19545 is also the number of non-defaulted arguments in TARGS.
19546 Record that. */
19547 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19548 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19549 }
19550 }
19551
19552 if (saw_undeduced++ == 1)
19553 goto again;
19554 }
19555
19556 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19557 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19558
19559 return unify_success (explain_p);
19560}
19561
19562/* Subroutine of type_unification_real. Args are like the variables
19563 at the call site. ARG is an overloaded function (or template-id);
19564 we try deducing template args from each of the overloads, and if
19565 only one succeeds, we go with that. Modifies TARGS and returns
19566 true on success. */
19567
19568static bool
19569resolve_overloaded_unification (tree tparms,
19570 tree targs,
19571 tree parm,
19572 tree arg,
19573 unification_kind_t strict,
19574 int sub_strict,
19575 bool explain_p)
19576{
19577 tree tempargs = copy_node (targs);
19578 int good = 0;
19579 tree goodfn = NULL_TREE;
19580 bool addr_p;
19581
19582 if (TREE_CODE (arg) == ADDR_EXPR)
19583 {
19584 arg = TREE_OPERAND (arg, 0);
19585 addr_p = true;
19586 }
19587 else
19588 addr_p = false;
19589
19590 if (TREE_CODE (arg) == COMPONENT_REF)
19591 /* Handle `&x' where `x' is some static or non-static member
19592 function name. */
19593 arg = TREE_OPERAND (arg, 1);
19594
19595 if (TREE_CODE (arg) == OFFSET_REF)
19596 arg = TREE_OPERAND (arg, 1);
19597
19598 /* Strip baselink information. */
19599 if (BASELINK_P (arg))
19600 arg = BASELINK_FUNCTIONS (arg);
19601
19602 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19603 {
19604 /* If we got some explicit template args, we need to plug them into
19605 the affected templates before we try to unify, in case the
19606 explicit args will completely resolve the templates in question. */
19607
19608 int ok = 0;
19609 tree expl_subargs = TREE_OPERAND (arg, 1);
19610 arg = TREE_OPERAND (arg, 0);
19611
19612 for (; arg; arg = OVL_NEXT (arg))
19613 {
19614 tree fn = OVL_CURRENT (arg);
19615 tree subargs, elem;
19616
19617 if (TREE_CODE (fn) != TEMPLATE_DECL)
19618 continue;
19619
19620 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19621 expl_subargs, NULL_TREE, tf_none,
19622 /*require_all_args=*/true,
19623 /*use_default_args=*/true);
19624 if (subargs != error_mark_node
19625 && !any_dependent_template_arguments_p (subargs))
19626 {
19627 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19628 if (try_one_overload (tparms, targs, tempargs, parm,
19629 elem, strict, sub_strict, addr_p, explain_p)
19630 && (!goodfn || !same_type_p (goodfn, elem)))
19631 {
19632 goodfn = elem;
19633 ++good;
19634 }
19635 }
19636 else if (subargs)
19637 ++ok;
19638 }
19639 /* If no templates (or more than one) are fully resolved by the
19640 explicit arguments, this template-id is a non-deduced context; it
19641 could still be OK if we deduce all template arguments for the
19642 enclosing call through other arguments. */
19643 if (good != 1)
19644 good = ok;
19645 }
19646 else if (TREE_CODE (arg) != OVERLOAD
19647 && TREE_CODE (arg) != FUNCTION_DECL)
19648 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19649 -- but the deduction does not succeed because the expression is
19650 not just the function on its own. */
19651 return false;
19652 else
19653 for (; arg; arg = OVL_NEXT (arg))
19654 if (try_one_overload (tparms, targs, tempargs, parm,
19655 TREE_TYPE (OVL_CURRENT (arg)),
19656 strict, sub_strict, addr_p, explain_p)
19657 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19658 {
19659 goodfn = OVL_CURRENT (arg);
19660 ++good;
19661 }
19662
19663 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19664 to function or pointer to member function argument if the set of
19665 overloaded functions does not contain function templates and at most
19666 one of a set of overloaded functions provides a unique match.
19667
19668 So if we found multiple possibilities, we return success but don't
19669 deduce anything. */
19670
19671 if (good == 1)
19672 {
19673 int i = TREE_VEC_LENGTH (targs);
19674 for (; i--; )
19675 if (TREE_VEC_ELT (tempargs, i))
19676 {
19677 tree old = TREE_VEC_ELT (targs, i);
19678 tree new_ = TREE_VEC_ELT (tempargs, i);
19679 if (new_ && old && ARGUMENT_PACK_P (old)
19680 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19681 /* Don't forget explicit template arguments in a pack. */
19682 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19683 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19684 TREE_VEC_ELT (targs, i) = new_;
19685 }
19686 }
19687 if (good)
19688 return true;
19689
19690 return false;
19691}
19692
19693/* Core DR 115: In contexts where deduction is done and fails, or in
19694 contexts where deduction is not done, if a template argument list is
19695 specified and it, along with any default template arguments, identifies
19696 a single function template specialization, then the template-id is an
19697 lvalue for the function template specialization. */
19698
19699tree
19700resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19701{
19702 tree expr, offset, baselink;
19703 bool addr;
19704
19705 if (!type_unknown_p (orig_expr))
19706 return orig_expr;
19707
19708 expr = orig_expr;
19709 addr = false;
19710 offset = NULL_TREE;
19711 baselink = NULL_TREE;
19712
19713 if (TREE_CODE (expr) == ADDR_EXPR)
19714 {
19715 expr = TREE_OPERAND (expr, 0);
19716 addr = true;
19717 }
19718 if (TREE_CODE (expr) == OFFSET_REF)
19719 {
19720 offset = expr;
19721 expr = TREE_OPERAND (expr, 1);
19722 }
19723 if (BASELINK_P (expr))
19724 {
19725 baselink = expr;
19726 expr = BASELINK_FUNCTIONS (expr);
19727 }
19728
19729 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19730 {
19731 int good = 0;
19732 tree goodfn = NULL_TREE;
19733
19734 /* If we got some explicit template args, we need to plug them into
19735 the affected templates before we try to unify, in case the
19736 explicit args will completely resolve the templates in question. */
19737
19738 tree expl_subargs = TREE_OPERAND (expr, 1);
19739 tree arg = TREE_OPERAND (expr, 0);
19740 tree badfn = NULL_TREE;
19741 tree badargs = NULL_TREE;
19742
19743 for (; arg; arg = OVL_NEXT (arg))
19744 {
19745 tree fn = OVL_CURRENT (arg);
19746 tree subargs, elem;
19747
19748 if (TREE_CODE (fn) != TEMPLATE_DECL)
19749 continue;
19750
19751 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19752 expl_subargs, NULL_TREE, tf_none,
19753 /*require_all_args=*/true,
19754 /*use_default_args=*/true);
19755 if (subargs != error_mark_node
19756 && !any_dependent_template_arguments_p (subargs))
19757 {
19758 elem = instantiate_template (fn, subargs, tf_none);
19759 if (elem == error_mark_node)
19760 {
19761 badfn = fn;
19762 badargs = subargs;
19763 }
19764 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19765 {
19766 goodfn = elem;
19767 ++good;
19768 }
19769 }
19770 }
19771 if (good == 1)
19772 {
19773 mark_used (goodfn);
19774 expr = goodfn;
19775 if (baselink)
19776 expr = build_baselink (BASELINK_BINFO (baselink),
19777 BASELINK_ACCESS_BINFO (baselink),
19778 expr, BASELINK_OPTYPE (baselink));
19779 if (offset)
19780 {
19781 tree base
19782 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19783 expr = build_offset_ref (base, expr, addr, complain);
19784 }
19785 if (addr)
19786 expr = cp_build_addr_expr (expr, complain);
19787 return expr;
19788 }
19789 else if (good == 0 && badargs && (complain & tf_error))
19790 /* There were no good options and at least one bad one, so let the
19791 user know what the problem is. */
19792 instantiate_template (badfn, badargs, complain);
19793 }
19794 return orig_expr;
19795}
19796
19797/* Subroutine of resolve_overloaded_unification; does deduction for a single
19798 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19799 different overloads deduce different arguments for a given parm.
19800 ADDR_P is true if the expression for which deduction is being
19801 performed was of the form "& fn" rather than simply "fn".
19802
19803 Returns 1 on success. */
19804
19805static int
19806try_one_overload (tree tparms,
19807 tree orig_targs,
19808 tree targs,
19809 tree parm,
19810 tree arg,
19811 unification_kind_t strict,
19812 int sub_strict,
19813 bool addr_p,
19814 bool explain_p)
19815{
19816 int nargs;
19817 tree tempargs;
19818 int i;
19819
19820 if (arg == error_mark_node)
19821 return 0;
19822
19823 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19824 to function or pointer to member function argument if the set of
19825 overloaded functions does not contain function templates and at most
19826 one of a set of overloaded functions provides a unique match.
19827
19828 So if this is a template, just return success. */
19829
19830 if (uses_template_parms (arg))
19831 return 1;
19832
19833 if (TREE_CODE (arg) == METHOD_TYPE)
19834 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19835 else if (addr_p)
19836 arg = build_pointer_type (arg);
19837
19838 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19839
19840 /* We don't copy orig_targs for this because if we have already deduced
19841 some template args from previous args, unify would complain when we
19842 try to deduce a template parameter for the same argument, even though
19843 there isn't really a conflict. */
19844 nargs = TREE_VEC_LENGTH (targs);
19845 tempargs = make_tree_vec (nargs);
19846
19847 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19848 return 0;
19849
19850 /* First make sure we didn't deduce anything that conflicts with
19851 explicitly specified args. */
19852 for (i = nargs; i--; )
19853 {
19854 tree elt = TREE_VEC_ELT (tempargs, i);
19855 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19856
19857 if (!elt)
19858 /*NOP*/;
19859 else if (uses_template_parms (elt))
19860 /* Since we're unifying against ourselves, we will fill in
19861 template args used in the function parm list with our own
19862 template parms. Discard them. */
19863 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19864 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19865 {
19866 /* Check that the argument at each index of the deduced argument pack
19867 is equivalent to the corresponding explicitly specified argument.
19868 We may have deduced more arguments than were explicitly specified,
19869 and that's OK. */
19870
19871 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19872 that's wrong if we deduce the same argument pack from multiple
19873 function arguments: it's only incomplete the first time. */
19874
19875 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19876 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19877
19878 if (TREE_VEC_LENGTH (deduced_pack)
19879 < TREE_VEC_LENGTH (explicit_pack))
19880 return 0;
19881
19882 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19883 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19884 TREE_VEC_ELT (deduced_pack, j)))
19885 return 0;
19886 }
19887 else if (oldelt && !template_args_equal (oldelt, elt))
19888 return 0;
19889 }
19890
19891 for (i = nargs; i--; )
19892 {
19893 tree elt = TREE_VEC_ELT (tempargs, i);
19894
19895 if (elt)
19896 TREE_VEC_ELT (targs, i) = elt;
19897 }
19898
19899 return 1;
19900}
19901
19902/* PARM is a template class (perhaps with unbound template
19903 parameters). ARG is a fully instantiated type. If ARG can be
19904 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19905 TARGS are as for unify. */
19906
19907static tree
19908try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19909 bool explain_p)
19910{
19911 tree copy_of_targs;
19912
19913 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19914 return NULL_TREE;
19915 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19916 /* Matches anything. */;
19917 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19918 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19919 return NULL_TREE;
19920
19921 /* We need to make a new template argument vector for the call to
19922 unify. If we used TARGS, we'd clutter it up with the result of
19923 the attempted unification, even if this class didn't work out.
19924 We also don't want to commit ourselves to all the unifications
19925 we've already done, since unification is supposed to be done on
19926 an argument-by-argument basis. In other words, consider the
19927 following pathological case:
19928
19929 template <int I, int J, int K>
19930 struct S {};
19931
19932 template <int I, int J>
19933 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19934
19935 template <int I, int J, int K>
19936 void f(S<I, J, K>, S<I, I, I>);
19937
19938 void g() {
19939 S<0, 0, 0> s0;
19940 S<0, 1, 2> s2;
19941
19942 f(s0, s2);
19943 }
19944
19945 Now, by the time we consider the unification involving `s2', we
19946 already know that we must have `f<0, 0, 0>'. But, even though
19947 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19948 because there are two ways to unify base classes of S<0, 1, 2>
19949 with S<I, I, I>. If we kept the already deduced knowledge, we
19950 would reject the possibility I=1. */
19951 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19952
19953 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19954 {
19955 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19956 return NULL_TREE;
19957 return arg;
19958 }
19959
19960 /* If unification failed, we're done. */
19961 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19962 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19963 return NULL_TREE;
19964
19965 return arg;
19966}
19967
19968/* Given a template type PARM and a class type ARG, find the unique
19969 base type in ARG that is an instance of PARM. We do not examine
19970 ARG itself; only its base-classes. If there is not exactly one
19971 appropriate base class, return NULL_TREE. PARM may be the type of
19972 a partial specialization, as well as a plain template type. Used
19973 by unify. */
19974
19975static enum template_base_result
19976get_template_base (tree tparms, tree targs, tree parm, tree arg,
19977 bool explain_p, tree *result)
19978{
19979 tree rval = NULL_TREE;
19980 tree binfo;
19981
19982 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19983
19984 binfo = TYPE_BINFO (complete_type (arg));
19985 if (!binfo)
19986 {
19987 /* The type could not be completed. */
19988 *result = NULL_TREE;
19989 return tbr_incomplete_type;
19990 }
19991
19992 /* Walk in inheritance graph order. The search order is not
19993 important, and this avoids multiple walks of virtual bases. */
19994 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19995 {
19996 tree r = try_class_unification (tparms, targs, parm,
19997 BINFO_TYPE (binfo), explain_p);
19998
19999 if (r)
20000 {
20001 /* If there is more than one satisfactory baseclass, then:
20002
20003 [temp.deduct.call]
20004
20005 If they yield more than one possible deduced A, the type
20006 deduction fails.
20007
20008 applies. */
20009 if (rval && !same_type_p (r, rval))
20010 {
20011 *result = NULL_TREE;
20012 return tbr_ambiguous_baseclass;
20013 }
20014
20015 rval = r;
20016 }
20017 }
20018
20019 *result = rval;
20020 return tbr_success;
20021}
20022
20023/* Returns the level of DECL, which declares a template parameter. */
20024
20025static int
20026template_decl_level (tree decl)
20027{
20028 switch (TREE_CODE (decl))
20029 {
20030 case TYPE_DECL:
20031 case TEMPLATE_DECL:
20032 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20033
20034 case PARM_DECL:
20035 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20036
20037 default:
20038 gcc_unreachable ();
20039 }
20040 return 0;
20041}
20042
20043/* Decide whether ARG can be unified with PARM, considering only the
20044 cv-qualifiers of each type, given STRICT as documented for unify.
20045 Returns nonzero iff the unification is OK on that basis. */
20046
20047static int
20048check_cv_quals_for_unify (int strict, tree arg, tree parm)
20049{
20050 int arg_quals = cp_type_quals (arg);
20051 int parm_quals = cp_type_quals (parm);
20052
20053 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20054 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20055 {
20056 /* Although a CVR qualifier is ignored when being applied to a
20057 substituted template parameter ([8.3.2]/1 for example), that
20058 does not allow us to unify "const T" with "int&" because both
20059 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20060 It is ok when we're allowing additional CV qualifiers
20061 at the outer level [14.8.2.1]/3,1st bullet. */
20062 if ((TREE_CODE (arg) == REFERENCE_TYPE
20063 || TREE_CODE (arg) == FUNCTION_TYPE
20064 || TREE_CODE (arg) == METHOD_TYPE)
20065 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20066 return 0;
20067
20068 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20069 && (parm_quals & TYPE_QUAL_RESTRICT))
20070 return 0;
20071 }
20072
20073 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20074 && (arg_quals & parm_quals) != parm_quals)
20075 return 0;
20076
20077 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20078 && (parm_quals & arg_quals) != arg_quals)
20079 return 0;
20080
20081 return 1;
20082}
20083
20084/* Determines the LEVEL and INDEX for the template parameter PARM. */
20085void
20086template_parm_level_and_index (tree parm, int* level, int* index)
20087{
20088 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20089 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20090 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20091 {
20092 *index = TEMPLATE_TYPE_IDX (parm);
20093 *level = TEMPLATE_TYPE_LEVEL (parm);
20094 }
20095 else
20096 {
20097 *index = TEMPLATE_PARM_IDX (parm);
20098 *level = TEMPLATE_PARM_LEVEL (parm);
20099 }
20100}
20101
20102#define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20103 do { \
20104 if (unify (TP, TA, P, A, S, EP)) \
20105 return 1; \
20106 } while (0)
20107
20108/* Unifies the remaining arguments in PACKED_ARGS with the pack
20109 expansion at the end of PACKED_PARMS. Returns 0 if the type
20110 deduction succeeds, 1 otherwise. STRICT is the same as in
20111 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20112 function call argument list. We'll need to adjust the arguments to make them
20113 types. SUBR tells us if this is from a recursive call to
20114 type_unification_real, or for comparing two template argument
20115 lists. */
20116
20117static int
20118unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20119 tree packed_args, unification_kind_t strict,
20120 bool subr, bool explain_p)
20121{
20122 tree parm
20123 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20124 tree pattern = PACK_EXPANSION_PATTERN (parm);
20125 tree pack, packs = NULL_TREE;
20126 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20127
20128 /* Add in any args remembered from an earlier partial instantiation. */
20129 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20130 int levels = TMPL_ARGS_DEPTH (targs);
20131
20132 packed_args = expand_template_argument_pack (packed_args);
20133
20134 int len = TREE_VEC_LENGTH (packed_args);
20135
20136 /* Determine the parameter packs we will be deducing from the
20137 pattern, and record their current deductions. */
20138 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20139 pack; pack = TREE_CHAIN (pack))
20140 {
20141 tree parm_pack = TREE_VALUE (pack);
20142 int idx, level;
20143
20144 /* Determine the index and level of this parameter pack. */
20145 template_parm_level_and_index (parm_pack, &level, &idx);
20146 if (level < levels)
20147 continue;
20148
20149 /* Keep track of the parameter packs and their corresponding
20150 argument packs. */
20151 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20152 TREE_TYPE (packs) = make_tree_vec (len - start);
20153 }
20154
20155 /* Loop through all of the arguments that have not yet been
20156 unified and unify each with the pattern. */
20157 for (i = start; i < len; i++)
20158 {
20159 tree parm;
20160 bool any_explicit = false;
20161 tree arg = TREE_VEC_ELT (packed_args, i);
20162
20163 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20164 or the element of its argument pack at the current index if
20165 this argument was explicitly specified. */
20166 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20167 {
20168 int idx, level;
20169 tree arg, pargs;
20170 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20171
20172 arg = NULL_TREE;
20173 if (TREE_VALUE (pack)
20174 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20175 && (i - start < TREE_VEC_LENGTH (pargs)))
20176 {
20177 any_explicit = true;
20178 arg = TREE_VEC_ELT (pargs, i - start);
20179 }
20180 TMPL_ARG (targs, level, idx) = arg;
20181 }
20182
20183 /* If we had explicit template arguments, substitute them into the
20184 pattern before deduction. */
20185 if (any_explicit)
20186 {
20187 /* Some arguments might still be unspecified or dependent. */
20188 bool dependent;
20189 ++processing_template_decl;
20190 dependent = any_dependent_template_arguments_p (targs);
20191 if (!dependent)
20192 --processing_template_decl;
20193 parm = tsubst (pattern, targs,
20194 explain_p ? tf_warning_or_error : tf_none,
20195 NULL_TREE);
20196 if (dependent)
20197 --processing_template_decl;
20198 if (parm == error_mark_node)
20199 return 1;
20200 }
20201 else
20202 parm = pattern;
20203
20204 /* Unify the pattern with the current argument. */
20205 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20206 explain_p))
20207 return 1;
20208
20209 /* For each parameter pack, collect the deduced value. */
20210 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20211 {
20212 int idx, level;
20213 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20214
20215 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20216 TMPL_ARG (targs, level, idx);
20217 }
20218 }
20219
20220 /* Verify that the results of unification with the parameter packs
20221 produce results consistent with what we've seen before, and make
20222 the deduced argument packs available. */
20223 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20224 {
20225 tree old_pack = TREE_VALUE (pack);
20226 tree new_args = TREE_TYPE (pack);
20227 int i, len = TREE_VEC_LENGTH (new_args);
20228 int idx, level;
20229 bool nondeduced_p = false;
20230
20231 /* By default keep the original deduced argument pack.
20232 If necessary, more specific code is going to update the
20233 resulting deduced argument later down in this function. */
20234 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20235 TMPL_ARG (targs, level, idx) = old_pack;
20236
20237 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20238 actually deduce anything. */
20239 for (i = 0; i < len && !nondeduced_p; ++i)
20240 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20241 nondeduced_p = true;
20242 if (nondeduced_p)
20243 continue;
20244
20245 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20246 {
20247 /* If we had fewer function args than explicit template args,
20248 just use the explicits. */
20249 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20250 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20251 if (len < explicit_len)
20252 new_args = explicit_args;
20253 }
20254
20255 if (!old_pack)
20256 {
20257 tree result;
20258 /* Build the deduced *_ARGUMENT_PACK. */
20259 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20260 {
20261 result = make_node (NONTYPE_ARGUMENT_PACK);
20262 TREE_TYPE (result) =
20263 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20264 TREE_CONSTANT (result) = 1;
20265 }
20266 else
20267 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20268
20269 SET_ARGUMENT_PACK_ARGS (result, new_args);
20270
20271 /* Note the deduced argument packs for this parameter
20272 pack. */
20273 TMPL_ARG (targs, level, idx) = result;
20274 }
20275 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20276 && (ARGUMENT_PACK_ARGS (old_pack)
20277 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20278 {
20279 /* We only had the explicitly-provided arguments before, but
20280 now we have a complete set of arguments. */
20281 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20282
20283 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20284 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20285 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20286 }
20287 else
20288 {
20289 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20290 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20291
20292 if (!comp_template_args (old_args, new_args,
20293 &bad_old_arg, &bad_new_arg))
20294 /* Inconsistent unification of this parameter pack. */
20295 return unify_parameter_pack_inconsistent (explain_p,
20296 bad_old_arg,
20297 bad_new_arg);
20298 }
20299 }
20300
20301 return unify_success (explain_p);
20302}
20303
20304/* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20305 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20306 parameters and return value are as for unify. */
20307
20308static int
20309unify_array_domain (tree tparms, tree targs,
20310 tree parm_dom, tree arg_dom,
20311 bool explain_p)
20312{
20313 tree parm_max;
20314 tree arg_max;
20315 bool parm_cst;
20316 bool arg_cst;
20317
20318 /* Our representation of array types uses "N - 1" as the
20319 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20320 not an integer constant. We cannot unify arbitrarily
20321 complex expressions, so we eliminate the MINUS_EXPRs
20322 here. */
20323 parm_max = TYPE_MAX_VALUE (parm_dom);
20324 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20325 if (!parm_cst)
20326 {
20327 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20328 parm_max = TREE_OPERAND (parm_max, 0);
20329 }
20330 arg_max = TYPE_MAX_VALUE (arg_dom);
20331 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20332 if (!arg_cst)
20333 {
20334 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20335 trying to unify the type of a variable with the type
20336 of a template parameter. For example:
20337
20338 template <unsigned int N>
20339 void f (char (&) [N]);
20340 int g();
20341 void h(int i) {
20342 char a[g(i)];
20343 f(a);
20344 }
20345
20346 Here, the type of the ARG will be "int [g(i)]", and
20347 may be a SAVE_EXPR, etc. */
20348 if (TREE_CODE (arg_max) != MINUS_EXPR)
20349 return unify_vla_arg (explain_p, arg_dom);
20350 arg_max = TREE_OPERAND (arg_max, 0);
20351 }
20352
20353 /* If only one of the bounds used a MINUS_EXPR, compensate
20354 by adding one to the other bound. */
20355 if (parm_cst && !arg_cst)
20356 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20357 integer_type_node,
20358 parm_max,
20359 integer_one_node);
20360 else if (arg_cst && !parm_cst)
20361 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20362 integer_type_node,
20363 arg_max,
20364 integer_one_node);
20365
20366 return unify (tparms, targs, parm_max, arg_max,
20367 UNIFY_ALLOW_INTEGER, explain_p);
20368}
20369
20370/* Returns whether T, a P or A in unify, is a type, template or expression. */
20371
20372enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20373
20374static pa_kind_t
20375pa_kind (tree t)
20376{
20377 if (PACK_EXPANSION_P (t))
20378 t = PACK_EXPANSION_PATTERN (t);
20379 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20380 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20381 || DECL_TYPE_TEMPLATE_P (t))
20382 return pa_tmpl;
20383 else if (TYPE_P (t))
20384 return pa_type;
20385 else
20386 return pa_expr;
20387}
20388
20389/* Deduce the value of template parameters. TPARMS is the (innermost)
20390 set of template parameters to a template. TARGS is the bindings
20391 for those template parameters, as determined thus far; TARGS may
20392 include template arguments for outer levels of template parameters
20393 as well. PARM is a parameter to a template function, or a
20394 subcomponent of that parameter; ARG is the corresponding argument.
20395 This function attempts to match PARM with ARG in a manner
20396 consistent with the existing assignments in TARGS. If more values
20397 are deduced, then TARGS is updated.
20398
20399 Returns 0 if the type deduction succeeds, 1 otherwise. The
20400 parameter STRICT is a bitwise or of the following flags:
20401
20402 UNIFY_ALLOW_NONE:
20403 Require an exact match between PARM and ARG.
20404 UNIFY_ALLOW_MORE_CV_QUAL:
20405 Allow the deduced ARG to be more cv-qualified (by qualification
20406 conversion) than ARG.
20407 UNIFY_ALLOW_LESS_CV_QUAL:
20408 Allow the deduced ARG to be less cv-qualified than ARG.
20409 UNIFY_ALLOW_DERIVED:
20410 Allow the deduced ARG to be a template base class of ARG,
20411 or a pointer to a template base class of the type pointed to by
20412 ARG.
20413 UNIFY_ALLOW_INTEGER:
20414 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20415 case for more information.
20416 UNIFY_ALLOW_OUTER_LEVEL:
20417 This is the outermost level of a deduction. Used to determine validity
20418 of qualification conversions. A valid qualification conversion must
20419 have const qualified pointers leading up to the inner type which
20420 requires additional CV quals, except at the outer level, where const
20421 is not required [conv.qual]. It would be normal to set this flag in
20422 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20423 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20424 This is the outermost level of a deduction, and PARM can be more CV
20425 qualified at this point.
20426 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20427 This is the outermost level of a deduction, and PARM can be less CV
20428 qualified at this point. */
20429
20430static int
20431unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20432 bool explain_p)
20433{
20434 int idx;
20435 tree targ;
20436 tree tparm;
20437 int strict_in = strict;
20438 tsubst_flags_t complain = (explain_p
20439 ? tf_warning_or_error
20440 : tf_none);
20441
20442 /* I don't think this will do the right thing with respect to types.
20443 But the only case I've seen it in so far has been array bounds, where
20444 signedness is the only information lost, and I think that will be
20445 okay. */
20446 while (CONVERT_EXPR_P (parm))
20447 parm = TREE_OPERAND (parm, 0);
20448
20449 if (arg == error_mark_node)
20450 return unify_invalid (explain_p);
20451 if (arg == unknown_type_node
20452 || arg == init_list_type_node)
20453 /* We can't deduce anything from this, but we might get all the
20454 template args from other function args. */
20455 return unify_success (explain_p);
20456
20457 if (parm == any_targ_node || arg == any_targ_node)
20458 return unify_success (explain_p);
20459
20460 /* If PARM uses template parameters, then we can't bail out here,
20461 even if ARG == PARM, since we won't record unifications for the
20462 template parameters. We might need them if we're trying to
20463 figure out which of two things is more specialized. */
20464 if (arg == parm && !uses_template_parms (parm))
20465 return unify_success (explain_p);
20466
20467 /* Handle init lists early, so the rest of the function can assume
20468 we're dealing with a type. */
20469 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20470 {
20471 tree elt, elttype;
20472 unsigned i;
20473 tree orig_parm = parm;
20474
20475 /* Replace T with std::initializer_list<T> for deduction. */
20476 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20477 && flag_deduce_init_list)
20478 parm = listify (parm);
20479
20480 if (!is_std_init_list (parm)
20481 && TREE_CODE (parm) != ARRAY_TYPE)
20482 /* We can only deduce from an initializer list argument if the
20483 parameter is std::initializer_list or an array; otherwise this
20484 is a non-deduced context. */
20485 return unify_success (explain_p);
20486
20487 if (TREE_CODE (parm) == ARRAY_TYPE)
20488 elttype = TREE_TYPE (parm);
20489 else
20490 {
20491 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20492 /* Deduction is defined in terms of a single type, so just punt
20493 on the (bizarre) std::initializer_list<T...>. */
20494 if (PACK_EXPANSION_P (elttype))
20495 return unify_success (explain_p);
20496 }
20497
20498 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20499 {
20500 int elt_strict = strict;
20501
20502 if (elt == error_mark_node)
20503 return unify_invalid (explain_p);
20504
20505 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20506 {
20507 tree type = TREE_TYPE (elt);
20508 if (type == error_mark_node)
20509 return unify_invalid (explain_p);
20510 /* It should only be possible to get here for a call. */
20511 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20512 elt_strict |= maybe_adjust_types_for_deduction
20513 (DEDUCE_CALL, &elttype, &type, elt);
20514 elt = type;
20515 }
20516
20517 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20518 explain_p);
20519 }
20520
20521 if (TREE_CODE (parm) == ARRAY_TYPE
20522 && deducible_array_bound (TYPE_DOMAIN (parm)))
20523 {
20524 /* Also deduce from the length of the initializer list. */
20525 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20526 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20527 if (idx == error_mark_node)
20528 return unify_invalid (explain_p);
20529 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20530 idx, explain_p);
20531 }
20532
20533 /* If the std::initializer_list<T> deduction worked, replace the
20534 deduced A with std::initializer_list<A>. */
20535 if (orig_parm != parm)
20536 {
20537 idx = TEMPLATE_TYPE_IDX (orig_parm);
20538 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20539 targ = listify (targ);
20540 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20541 }
20542 return unify_success (explain_p);
20543 }
20544
20545 /* If parm and arg aren't the same kind of thing (template, type, or
20546 expression), fail early. */
20547 if (pa_kind (parm) != pa_kind (arg))
20548 return unify_invalid (explain_p);
20549
20550 /* Immediately reject some pairs that won't unify because of
20551 cv-qualification mismatches. */
20552 if (TREE_CODE (arg) == TREE_CODE (parm)
20553 && TYPE_P (arg)
20554 /* It is the elements of the array which hold the cv quals of an array
20555 type, and the elements might be template type parms. We'll check
20556 when we recurse. */
20557 && TREE_CODE (arg) != ARRAY_TYPE
20558 /* We check the cv-qualifiers when unifying with template type
20559 parameters below. We want to allow ARG `const T' to unify with
20560 PARM `T' for example, when computing which of two templates
20561 is more specialized, for example. */
20562 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20563 && !check_cv_quals_for_unify (strict_in, arg, parm))
20564 return unify_cv_qual_mismatch (explain_p, parm, arg);
20565
20566 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20567 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20568 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20569 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20570 strict &= ~UNIFY_ALLOW_DERIVED;
20571 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20572 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20573
20574 switch (TREE_CODE (parm))
20575 {
20576 case TYPENAME_TYPE:
20577 case SCOPE_REF:
20578 case UNBOUND_CLASS_TEMPLATE:
20579 /* In a type which contains a nested-name-specifier, template
20580 argument values cannot be deduced for template parameters used
20581 within the nested-name-specifier. */
20582 return unify_success (explain_p);
20583
20584 case TEMPLATE_TYPE_PARM:
20585 case TEMPLATE_TEMPLATE_PARM:
20586 case BOUND_TEMPLATE_TEMPLATE_PARM:
20587 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20588 if (error_operand_p (tparm))
20589 return unify_invalid (explain_p);
20590
20591 if (TEMPLATE_TYPE_LEVEL (parm)
20592 != template_decl_level (tparm))
20593 /* The PARM is not one we're trying to unify. Just check
20594 to see if it matches ARG. */
20595 {
20596 if (TREE_CODE (arg) == TREE_CODE (parm)
20597 && (is_auto (parm) ? is_auto (arg)
20598 : same_type_p (parm, arg)))
20599 return unify_success (explain_p);
20600 else
20601 return unify_type_mismatch (explain_p, parm, arg);
20602 }
20603 idx = TEMPLATE_TYPE_IDX (parm);
20604 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20605 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20606 if (error_operand_p (tparm))
20607 return unify_invalid (explain_p);
20608
20609 /* Check for mixed types and values. */
20610 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20611 && TREE_CODE (tparm) != TYPE_DECL)
20612 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20613 && TREE_CODE (tparm) != TEMPLATE_DECL))
20614 gcc_unreachable ();
20615
20616 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20617 {
20618 if ((strict_in & UNIFY_ALLOW_DERIVED)
20619 && CLASS_TYPE_P (arg))
20620 {
20621 /* First try to match ARG directly. */
20622 tree t = try_class_unification (tparms, targs, parm, arg,
20623 explain_p);
20624 if (!t)
20625 {
20626 /* Otherwise, look for a suitable base of ARG, as below. */
20627 enum template_base_result r;
20628 r = get_template_base (tparms, targs, parm, arg,
20629 explain_p, &t);
20630 if (!t)
20631 return unify_no_common_base (explain_p, r, parm, arg);
20632 arg = t;
20633 }
20634 }
20635 /* ARG must be constructed from a template class or a template
20636 template parameter. */
20637 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20638 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20639 return unify_template_deduction_failure (explain_p, parm, arg);
20640
20641 /* Deduce arguments T, i from TT<T> or TT<i>. */
20642 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20643 return 1;
20644
20645 arg = TYPE_TI_TEMPLATE (arg);
20646
20647 /* Fall through to deduce template name. */
20648 }
20649
20650 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20651 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20652 {
20653 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20654
20655 /* Simple cases: Value already set, does match or doesn't. */
20656 if (targ != NULL_TREE && template_args_equal (targ, arg))
20657 return unify_success (explain_p);
20658 else if (targ)
20659 return unify_inconsistency (explain_p, parm, targ, arg);
20660 }
20661 else
20662 {
20663 /* If PARM is `const T' and ARG is only `int', we don't have
20664 a match unless we are allowing additional qualification.
20665 If ARG is `const int' and PARM is just `T' that's OK;
20666 that binds `const int' to `T'. */
20667 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20668 arg, parm))
20669 return unify_cv_qual_mismatch (explain_p, parm, arg);
20670
20671 /* Consider the case where ARG is `const volatile int' and
20672 PARM is `const T'. Then, T should be `volatile int'. */
20673 arg = cp_build_qualified_type_real
20674 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20675 if (arg == error_mark_node)
20676 return unify_invalid (explain_p);
20677
20678 /* Simple cases: Value already set, does match or doesn't. */
20679 if (targ != NULL_TREE && same_type_p (targ, arg))
20680 return unify_success (explain_p);
20681 else if (targ)
20682 return unify_inconsistency (explain_p, parm, targ, arg);
20683
20684 /* Make sure that ARG is not a variable-sized array. (Note
20685 that were talking about variable-sized arrays (like
20686 `int[n]'), rather than arrays of unknown size (like
20687 `int[]').) We'll get very confused by such a type since
20688 the bound of the array is not constant, and therefore
20689 not mangleable. Besides, such types are not allowed in
20690 ISO C++, so we can do as we please here. We do allow
20691 them for 'auto' deduction, since that isn't ABI-exposed. */
20692 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20693 return unify_vla_arg (explain_p, arg);
20694
20695 /* Strip typedefs as in convert_template_argument. */
20696 arg = canonicalize_type_argument (arg, tf_none);
20697 }
20698
20699 /* If ARG is a parameter pack or an expansion, we cannot unify
20700 against it unless PARM is also a parameter pack. */
20701 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20702 && !template_parameter_pack_p (parm))
20703 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20704
20705 /* If the argument deduction results is a METHOD_TYPE,
20706 then there is a problem.
20707 METHOD_TYPE doesn't map to any real C++ type the result of
20708 the deduction can not be of that type. */
20709 if (TREE_CODE (arg) == METHOD_TYPE)
20710 return unify_method_type_error (explain_p, arg);
20711
20712 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20713 return unify_success (explain_p);
20714
20715 case TEMPLATE_PARM_INDEX:
20716 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20717 if (error_operand_p (tparm))
20718 return unify_invalid (explain_p);
20719
20720 if (TEMPLATE_PARM_LEVEL (parm)
20721 != template_decl_level (tparm))
20722 {
20723 /* The PARM is not one we're trying to unify. Just check
20724 to see if it matches ARG. */
20725 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20726 && cp_tree_equal (parm, arg));
20727 if (result)
20728 unify_expression_unequal (explain_p, parm, arg);
20729 return result;
20730 }
20731
20732 idx = TEMPLATE_PARM_IDX (parm);
20733 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20734
20735 if (targ)
20736 {
20737 int x = !cp_tree_equal (targ, arg);
20738 if (x)
20739 unify_inconsistency (explain_p, parm, targ, arg);
20740 return x;
20741 }
20742
20743 /* [temp.deduct.type] If, in the declaration of a function template
20744 with a non-type template-parameter, the non-type
20745 template-parameter is used in an expression in the function
20746 parameter-list and, if the corresponding template-argument is
20747 deduced, the template-argument type shall match the type of the
20748 template-parameter exactly, except that a template-argument
20749 deduced from an array bound may be of any integral type.
20750 The non-type parameter might use already deduced type parameters. */
20751 ++processing_template_decl;
20752 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20753 --processing_template_decl;
20754 if (tree a = type_uses_auto (tparm))
20755 {
20756 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20757 if (tparm == error_mark_node)
20758 return 1;
20759 }
20760
20761 if (!TREE_TYPE (arg))
20762 /* Template-parameter dependent expression. Just accept it for now.
20763 It will later be processed in convert_template_argument. */
20764 ;
20765 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20766 non_reference (tparm)))
20767 /* OK */;
20768 else if ((strict & UNIFY_ALLOW_INTEGER)
20769 && CP_INTEGRAL_TYPE_P (tparm))
20770 /* Convert the ARG to the type of PARM; the deduced non-type
20771 template argument must exactly match the types of the
20772 corresponding parameter. */
20773 arg = fold (build_nop (tparm, arg));
20774 else if (uses_template_parms (tparm))
20775 {
20776 /* We haven't deduced the type of this parameter yet. */
20777 if (cxx_dialect >= cxx1z
20778 /* We deduce from array bounds in try_array_deduction. */
20779 && !(strict & UNIFY_ALLOW_INTEGER))
20780 {
20781 /* Deduce it from the non-type argument. */
20782 tree atype = TREE_TYPE (arg);
20783 RECUR_AND_CHECK_FAILURE (tparms, targs,
20784 tparm, atype,
20785 UNIFY_ALLOW_NONE, explain_p);
20786 }
20787 else
20788 /* Try again later. */
20789 return unify_success (explain_p);
20790 }
20791 else
20792 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20793
20794 /* If ARG is a parameter pack or an expansion, we cannot unify
20795 against it unless PARM is also a parameter pack. */
20796 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20797 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20798 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20799
20800 {
20801 bool removed_attr = false;
20802 arg = strip_typedefs_expr (arg, &removed_attr);
20803 }
20804 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20805 return unify_success (explain_p);
20806
20807 case PTRMEM_CST:
20808 {
20809 /* A pointer-to-member constant can be unified only with
20810 another constant. */
20811 if (TREE_CODE (arg) != PTRMEM_CST)
20812 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20813
20814 /* Just unify the class member. It would be useless (and possibly
20815 wrong, depending on the strict flags) to unify also
20816 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20817 arg refer to the same variable, even if through different
20818 classes. For instance:
20819
20820 struct A { int x; };
20821 struct B : A { };
20822
20823 Unification of &A::x and &B::x must succeed. */
20824 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20825 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20826 }
20827
20828 case POINTER_TYPE:
20829 {
20830 if (!TYPE_PTR_P (arg))
20831 return unify_type_mismatch (explain_p, parm, arg);
20832
20833 /* [temp.deduct.call]
20834
20835 A can be another pointer or pointer to member type that can
20836 be converted to the deduced A via a qualification
20837 conversion (_conv.qual_).
20838
20839 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20840 This will allow for additional cv-qualification of the
20841 pointed-to types if appropriate. */
20842
20843 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20844 /* The derived-to-base conversion only persists through one
20845 level of pointers. */
20846 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20847
20848 return unify (tparms, targs, TREE_TYPE (parm),
20849 TREE_TYPE (arg), strict, explain_p);
20850 }
20851
20852 case REFERENCE_TYPE:
20853 if (TREE_CODE (arg) != REFERENCE_TYPE)
20854 return unify_type_mismatch (explain_p, parm, arg);
20855 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20856 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20857
20858 case ARRAY_TYPE:
20859 if (TREE_CODE (arg) != ARRAY_TYPE)
20860 return unify_type_mismatch (explain_p, parm, arg);
20861 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20862 != (TYPE_DOMAIN (arg) == NULL_TREE))
20863 return unify_type_mismatch (explain_p, parm, arg);
20864 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20865 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20866 if (TYPE_DOMAIN (parm) != NULL_TREE)
20867 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20868 TYPE_DOMAIN (arg), explain_p);
20869 return unify_success (explain_p);
20870
20871 case REAL_TYPE:
20872 case COMPLEX_TYPE:
20873 case VECTOR_TYPE:
20874 case INTEGER_TYPE:
20875 case BOOLEAN_TYPE:
20876 case ENUMERAL_TYPE:
20877 case VOID_TYPE:
20878 case NULLPTR_TYPE:
20879 if (TREE_CODE (arg) != TREE_CODE (parm))
20880 return unify_type_mismatch (explain_p, parm, arg);
20881
20882 /* We have already checked cv-qualification at the top of the
20883 function. */
20884 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20885 return unify_type_mismatch (explain_p, parm, arg);
20886
20887 /* As far as unification is concerned, this wins. Later checks
20888 will invalidate it if necessary. */
20889 return unify_success (explain_p);
20890
20891 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20892 /* Type INTEGER_CST can come from ordinary constant template args. */
20893 case INTEGER_CST:
20894 while (CONVERT_EXPR_P (arg))
20895 arg = TREE_OPERAND (arg, 0);
20896
20897 if (TREE_CODE (arg) != INTEGER_CST)
20898 return unify_template_argument_mismatch (explain_p, parm, arg);
20899 return (tree_int_cst_equal (parm, arg)
20900 ? unify_success (explain_p)
20901 : unify_template_argument_mismatch (explain_p, parm, arg));
20902
20903 case TREE_VEC:
20904 {
20905 int i, len, argslen;
20906 int parm_variadic_p = 0;
20907
20908 if (TREE_CODE (arg) != TREE_VEC)
20909 return unify_template_argument_mismatch (explain_p, parm, arg);
20910
20911 len = TREE_VEC_LENGTH (parm);
20912 argslen = TREE_VEC_LENGTH (arg);
20913
20914 /* Check for pack expansions in the parameters. */
20915 for (i = 0; i < len; ++i)
20916 {
20917 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20918 {
20919 if (i == len - 1)
20920 /* We can unify against something with a trailing
20921 parameter pack. */
20922 parm_variadic_p = 1;
20923 else
20924 /* [temp.deduct.type]/9: If the template argument list of
20925 P contains a pack expansion that is not the last
20926 template argument, the entire template argument list
20927 is a non-deduced context. */
20928 return unify_success (explain_p);
20929 }
20930 }
20931
20932 /* If we don't have enough arguments to satisfy the parameters
20933 (not counting the pack expression at the end), or we have
20934 too many arguments for a parameter list that doesn't end in
20935 a pack expression, we can't unify. */
20936 if (parm_variadic_p
20937 ? argslen < len - parm_variadic_p
20938 : argslen != len)
20939 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20940
20941 /* Unify all of the parameters that precede the (optional)
20942 pack expression. */
20943 for (i = 0; i < len - parm_variadic_p; ++i)
20944 {
20945 RECUR_AND_CHECK_FAILURE (tparms, targs,
20946 TREE_VEC_ELT (parm, i),
20947 TREE_VEC_ELT (arg, i),
20948 UNIFY_ALLOW_NONE, explain_p);
20949 }
20950 if (parm_variadic_p)
20951 return unify_pack_expansion (tparms, targs, parm, arg,
20952 DEDUCE_EXACT,
20953 /*subr=*/true, explain_p);
20954 return unify_success (explain_p);
20955 }
20956
20957 case RECORD_TYPE:
20958 case UNION_TYPE:
20959 if (TREE_CODE (arg) != TREE_CODE (parm))
20960 return unify_type_mismatch (explain_p, parm, arg);
20961
20962 if (TYPE_PTRMEMFUNC_P (parm))
20963 {
20964 if (!TYPE_PTRMEMFUNC_P (arg))
20965 return unify_type_mismatch (explain_p, parm, arg);
20966
20967 return unify (tparms, targs,
20968 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20969 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20970 strict, explain_p);
20971 }
20972 else if (TYPE_PTRMEMFUNC_P (arg))
20973 return unify_type_mismatch (explain_p, parm, arg);
20974
20975 if (CLASSTYPE_TEMPLATE_INFO (parm))
20976 {
20977 tree t = NULL_TREE;
20978
20979 if (strict_in & UNIFY_ALLOW_DERIVED)
20980 {
20981 /* First, we try to unify the PARM and ARG directly. */
20982 t = try_class_unification (tparms, targs,
20983 parm, arg, explain_p);
20984
20985 if (!t)
20986 {
20987 /* Fallback to the special case allowed in
20988 [temp.deduct.call]:
20989
20990 If P is a class, and P has the form
20991 template-id, then A can be a derived class of
20992 the deduced A. Likewise, if P is a pointer to
20993 a class of the form template-id, A can be a
20994 pointer to a derived class pointed to by the
20995 deduced A. */
20996 enum template_base_result r;
20997 r = get_template_base (tparms, targs, parm, arg,
20998 explain_p, &t);
20999
21000 if (!t)
21001 {
21002 /* Don't give the derived diagnostic if we're
21003 already dealing with the same template. */
21004 bool same_template
21005 = (CLASSTYPE_TEMPLATE_INFO (arg)
21006 && (CLASSTYPE_TI_TEMPLATE (parm)
21007 == CLASSTYPE_TI_TEMPLATE (arg)));
21008 return unify_no_common_base (explain_p && !same_template,
21009 r, parm, arg);
21010 }
21011 }
21012 }
21013 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21014 && (CLASSTYPE_TI_TEMPLATE (parm)
21015 == CLASSTYPE_TI_TEMPLATE (arg)))
21016 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21017 Then, we should unify `int' and `U'. */
21018 t = arg;
21019 else
21020 /* There's no chance of unification succeeding. */
21021 return unify_type_mismatch (explain_p, parm, arg);
21022
21023 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21024 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21025 }
21026 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21027 return unify_type_mismatch (explain_p, parm, arg);
21028 return unify_success (explain_p);
21029
21030 case METHOD_TYPE:
21031 case FUNCTION_TYPE:
21032 {
21033 unsigned int nargs;
21034 tree *args;
21035 tree a;
21036 unsigned int i;
21037
21038 if (TREE_CODE (arg) != TREE_CODE (parm))
21039 return unify_type_mismatch (explain_p, parm, arg);
21040
21041 /* CV qualifications for methods can never be deduced, they must
21042 match exactly. We need to check them explicitly here,
21043 because type_unification_real treats them as any other
21044 cv-qualified parameter. */
21045 if (TREE_CODE (parm) == METHOD_TYPE
21046 && (!check_cv_quals_for_unify
21047 (UNIFY_ALLOW_NONE,
21048 class_of_this_parm (arg),
21049 class_of_this_parm (parm))))
21050 return unify_cv_qual_mismatch (explain_p, parm, arg);
21051 if (TREE_CODE (arg) == FUNCTION_TYPE
21052 && type_memfn_quals (parm) != type_memfn_quals (arg))
21053 return unify_cv_qual_mismatch (explain_p, parm, arg);
21054 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21055 return unify_type_mismatch (explain_p, parm, arg);
21056
21057 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21058 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21059
21060 nargs = list_length (TYPE_ARG_TYPES (arg));
21061 args = XALLOCAVEC (tree, nargs);
21062 for (a = TYPE_ARG_TYPES (arg), i = 0;
21063 a != NULL_TREE && a != void_list_node;
21064 a = TREE_CHAIN (a), ++i)
21065 args[i] = TREE_VALUE (a);
21066 nargs = i;
21067
21068 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21069 args, nargs, 1, DEDUCE_EXACT,
21070 LOOKUP_NORMAL, NULL, explain_p))
21071 return 1;
21072
21073 if (flag_noexcept_type)
21074 {
21075 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21076 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21077 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21078 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21079 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21080 && uses_template_parms (TREE_PURPOSE (pspec)))
21081 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21082 TREE_PURPOSE (aspec),
21083 UNIFY_ALLOW_NONE, explain_p);
21084 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21085 return unify_type_mismatch (explain_p, parm, arg);
21086 }
21087
21088 return 0;
21089 }
21090
21091 case OFFSET_TYPE:
21092 /* Unify a pointer to member with a pointer to member function, which
21093 deduces the type of the member as a function type. */
21094 if (TYPE_PTRMEMFUNC_P (arg))
21095 {
21096 /* Check top-level cv qualifiers */
21097 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21098 return unify_cv_qual_mismatch (explain_p, parm, arg);
21099
21100 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21101 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21102 UNIFY_ALLOW_NONE, explain_p);
21103
21104 /* Determine the type of the function we are unifying against. */
21105 tree fntype = static_fn_type (arg);
21106
21107 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21108 }
21109
21110 if (TREE_CODE (arg) != OFFSET_TYPE)
21111 return unify_type_mismatch (explain_p, parm, arg);
21112 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21113 TYPE_OFFSET_BASETYPE (arg),
21114 UNIFY_ALLOW_NONE, explain_p);
21115 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21116 strict, explain_p);
21117
21118 case CONST_DECL:
21119 if (DECL_TEMPLATE_PARM_P (parm))
21120 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21121 if (arg != scalar_constant_value (parm))
21122 return unify_template_argument_mismatch (explain_p, parm, arg);
21123 return unify_success (explain_p);
21124
21125 case FIELD_DECL:
21126 case TEMPLATE_DECL:
21127 /* Matched cases are handled by the ARG == PARM test above. */
21128 return unify_template_argument_mismatch (explain_p, parm, arg);
21129
21130 case VAR_DECL:
21131 /* We might get a variable as a non-type template argument in parm if the
21132 corresponding parameter is type-dependent. Make any necessary
21133 adjustments based on whether arg is a reference. */
21134 if (CONSTANT_CLASS_P (arg))
21135 parm = fold_non_dependent_expr (parm);
21136 else if (REFERENCE_REF_P (arg))
21137 {
21138 tree sub = TREE_OPERAND (arg, 0);
21139 STRIP_NOPS (sub);
21140 if (TREE_CODE (sub) == ADDR_EXPR)
21141 arg = TREE_OPERAND (sub, 0);
21142 }
21143 /* Now use the normal expression code to check whether they match. */
21144 goto expr;
21145
21146 case TYPE_ARGUMENT_PACK:
21147 case NONTYPE_ARGUMENT_PACK:
21148 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21149 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21150
21151 case TYPEOF_TYPE:
21152 case DECLTYPE_TYPE:
21153 case UNDERLYING_TYPE:
21154 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21155 or UNDERLYING_TYPE nodes. */
21156 return unify_success (explain_p);
21157
21158 case ERROR_MARK:
21159 /* Unification fails if we hit an error node. */
21160 return unify_invalid (explain_p);
21161
21162 case INDIRECT_REF:
21163 if (REFERENCE_REF_P (parm))
21164 {
21165 bool pexp = PACK_EXPANSION_P (arg);
21166 if (pexp)
21167 arg = PACK_EXPANSION_PATTERN (arg);
21168 if (REFERENCE_REF_P (arg))
21169 arg = TREE_OPERAND (arg, 0);
21170 if (pexp)
21171 arg = make_pack_expansion (arg);
21172 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21173 strict, explain_p);
21174 }
21175 /* FALLTHRU */
21176
21177 default:
21178 /* An unresolved overload is a nondeduced context. */
21179 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21180 return unify_success (explain_p);
21181 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21182 expr:
21183 /* We must be looking at an expression. This can happen with
21184 something like:
21185
21186 template <int I>
21187 void foo(S<I>, S<I + 2>);
21188
21189 This is a "nondeduced context":
21190
21191 [deduct.type]
21192
21193 The nondeduced contexts are:
21194
21195 --A type that is a template-id in which one or more of
21196 the template-arguments is an expression that references
21197 a template-parameter.
21198
21199 In these cases, we assume deduction succeeded, but don't
21200 actually infer any unifications. */
21201
21202 if (!uses_template_parms (parm)
21203 && !template_args_equal (parm, arg))
21204 return unify_expression_unequal (explain_p, parm, arg);
21205 else
21206 return unify_success (explain_p);
21207 }
21208}
21209#undef RECUR_AND_CHECK_FAILURE
21210
21211/* Note that DECL can be defined in this translation unit, if
21212 required. */
21213
21214static void
21215mark_definable (tree decl)
21216{
21217 tree clone;
21218 DECL_NOT_REALLY_EXTERN (decl) = 1;
21219 FOR_EACH_CLONE (clone, decl)
21220 DECL_NOT_REALLY_EXTERN (clone) = 1;
21221}
21222
21223/* Called if RESULT is explicitly instantiated, or is a member of an
21224 explicitly instantiated class. */
21225
21226void
21227mark_decl_instantiated (tree result, int extern_p)
21228{
21229 SET_DECL_EXPLICIT_INSTANTIATION (result);
21230
21231 /* If this entity has already been written out, it's too late to
21232 make any modifications. */
21233 if (TREE_ASM_WRITTEN (result))
21234 return;
21235
21236 /* For anonymous namespace we don't need to do anything. */
21237 if (decl_anon_ns_mem_p (result))
21238 {
21239 gcc_assert (!TREE_PUBLIC (result));
21240 return;
21241 }
21242
21243 if (TREE_CODE (result) != FUNCTION_DECL)
21244 /* The TREE_PUBLIC flag for function declarations will have been
21245 set correctly by tsubst. */
21246 TREE_PUBLIC (result) = 1;
21247
21248 /* This might have been set by an earlier implicit instantiation. */
21249 DECL_COMDAT (result) = 0;
21250
21251 if (extern_p)
21252 DECL_NOT_REALLY_EXTERN (result) = 0;
21253 else
21254 {
21255 mark_definable (result);
21256 mark_needed (result);
21257 /* Always make artificials weak. */
21258 if (DECL_ARTIFICIAL (result) && flag_weak)
21259 comdat_linkage (result);
21260 /* For WIN32 we also want to put explicit instantiations in
21261 linkonce sections. */
21262 else if (TREE_PUBLIC (result))
21263 maybe_make_one_only (result);
21264 }
21265
21266 /* If EXTERN_P, then this function will not be emitted -- unless
21267 followed by an explicit instantiation, at which point its linkage
21268 will be adjusted. If !EXTERN_P, then this function will be
21269 emitted here. In neither circumstance do we want
21270 import_export_decl to adjust the linkage. */
21271 DECL_INTERFACE_KNOWN (result) = 1;
21272}
21273
21274/* Subroutine of more_specialized_fn: check whether TARGS is missing any
21275 important template arguments. If any are missing, we check whether
21276 they're important by using error_mark_node for substituting into any
21277 args that were used for partial ordering (the ones between ARGS and END)
21278 and seeing if it bubbles up. */
21279
21280static bool
21281check_undeduced_parms (tree targs, tree args, tree end)
21282{
21283 bool found = false;
21284 int i;
21285 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21286 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21287 {
21288 found = true;
21289 TREE_VEC_ELT (targs, i) = error_mark_node;
21290 }
21291 if (found)
21292 {
21293 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21294 if (substed == error_mark_node)
21295 return true;
21296 }
21297 return false;
21298}
21299
21300/* Given two function templates PAT1 and PAT2, return:
21301
21302 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21303 -1 if PAT2 is more specialized than PAT1.
21304 0 if neither is more specialized.
21305
21306 LEN indicates the number of parameters we should consider
21307 (defaulted parameters should not be considered).
21308
21309 The 1998 std underspecified function template partial ordering, and
21310 DR214 addresses the issue. We take pairs of arguments, one from
21311 each of the templates, and deduce them against each other. One of
21312 the templates will be more specialized if all the *other*
21313 template's arguments deduce against its arguments and at least one
21314 of its arguments *does* *not* deduce against the other template's
21315 corresponding argument. Deduction is done as for class templates.
21316 The arguments used in deduction have reference and top level cv
21317 qualifiers removed. Iff both arguments were originally reference
21318 types *and* deduction succeeds in both directions, an lvalue reference
21319 wins against an rvalue reference and otherwise the template
21320 with the more cv-qualified argument wins for that pairing (if
21321 neither is more cv-qualified, they both are equal). Unlike regular
21322 deduction, after all the arguments have been deduced in this way,
21323 we do *not* verify the deduced template argument values can be
21324 substituted into non-deduced contexts.
21325
21326 The logic can be a bit confusing here, because we look at deduce1 and
21327 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21328 can find template arguments for pat1 to make arg1 look like arg2, that
21329 means that arg2 is at least as specialized as arg1. */
21330
21331int
21332more_specialized_fn (tree pat1, tree pat2, int len)
21333{
21334 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21335 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21336 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21337 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21338 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21339 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21340 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21341 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21342 tree origs1, origs2;
21343 bool lose1 = false;
21344 bool lose2 = false;
21345
21346 /* Remove the this parameter from non-static member functions. If
21347 one is a non-static member function and the other is not a static
21348 member function, remove the first parameter from that function
21349 also. This situation occurs for operator functions where we
21350 locate both a member function (with this pointer) and non-member
21351 operator (with explicit first operand). */
21352 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21353 {
21354 len--; /* LEN is the number of significant arguments for DECL1 */
21355 args1 = TREE_CHAIN (args1);
21356 if (!DECL_STATIC_FUNCTION_P (decl2))
21357 args2 = TREE_CHAIN (args2);
21358 }
21359 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21360 {
21361 args2 = TREE_CHAIN (args2);
21362 if (!DECL_STATIC_FUNCTION_P (decl1))
21363 {
21364 len--;
21365 args1 = TREE_CHAIN (args1);
21366 }
21367 }
21368
21369 /* If only one is a conversion operator, they are unordered. */
21370 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21371 return 0;
21372
21373 /* Consider the return type for a conversion function */
21374 if (DECL_CONV_FN_P (decl1))
21375 {
21376 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21377 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21378 len++;
21379 }
21380
21381 processing_template_decl++;
21382
21383 origs1 = args1;
21384 origs2 = args2;
21385
21386 while (len--
21387 /* Stop when an ellipsis is seen. */
21388 && args1 != NULL_TREE && args2 != NULL_TREE)
21389 {
21390 tree arg1 = TREE_VALUE (args1);
21391 tree arg2 = TREE_VALUE (args2);
21392 int deduce1, deduce2;
21393 int quals1 = -1;
21394 int quals2 = -1;
21395 int ref1 = 0;
21396 int ref2 = 0;
21397
21398 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21399 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21400 {
21401 /* When both arguments are pack expansions, we need only
21402 unify the patterns themselves. */
21403 arg1 = PACK_EXPANSION_PATTERN (arg1);
21404 arg2 = PACK_EXPANSION_PATTERN (arg2);
21405
21406 /* This is the last comparison we need to do. */
21407 len = 0;
21408 }
21409
21410 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21411 {
21412 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21413 arg1 = TREE_TYPE (arg1);
21414 quals1 = cp_type_quals (arg1);
21415 }
21416
21417 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21418 {
21419 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21420 arg2 = TREE_TYPE (arg2);
21421 quals2 = cp_type_quals (arg2);
21422 }
21423
21424 arg1 = TYPE_MAIN_VARIANT (arg1);
21425 arg2 = TYPE_MAIN_VARIANT (arg2);
21426
21427 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21428 {
21429 int i, len2 = remaining_arguments (args2);
21430 tree parmvec = make_tree_vec (1);
21431 tree argvec = make_tree_vec (len2);
21432 tree ta = args2;
21433
21434 /* Setup the parameter vector, which contains only ARG1. */
21435 TREE_VEC_ELT (parmvec, 0) = arg1;
21436
21437 /* Setup the argument vector, which contains the remaining
21438 arguments. */
21439 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21440 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21441
21442 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21443 argvec, DEDUCE_EXACT,
21444 /*subr=*/true, /*explain_p=*/false)
21445 == 0);
21446
21447 /* We cannot deduce in the other direction, because ARG1 is
21448 a pack expansion but ARG2 is not. */
21449 deduce2 = 0;
21450 }
21451 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21452 {
21453 int i, len1 = remaining_arguments (args1);
21454 tree parmvec = make_tree_vec (1);
21455 tree argvec = make_tree_vec (len1);
21456 tree ta = args1;
21457
21458 /* Setup the parameter vector, which contains only ARG1. */
21459 TREE_VEC_ELT (parmvec, 0) = arg2;
21460
21461 /* Setup the argument vector, which contains the remaining
21462 arguments. */
21463 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21464 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21465
21466 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21467 argvec, DEDUCE_EXACT,
21468 /*subr=*/true, /*explain_p=*/false)
21469 == 0);
21470
21471 /* We cannot deduce in the other direction, because ARG2 is
21472 a pack expansion but ARG1 is not.*/
21473 deduce1 = 0;
21474 }
21475
21476 else
21477 {
21478 /* The normal case, where neither argument is a pack
21479 expansion. */
21480 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21481 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21482 == 0);
21483 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21484 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21485 == 0);
21486 }
21487
21488 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21489 arg2, then arg2 is not as specialized as arg1. */
21490 if (!deduce1)
21491 lose2 = true;
21492 if (!deduce2)
21493 lose1 = true;
21494
21495 /* "If, for a given type, deduction succeeds in both directions
21496 (i.e., the types are identical after the transformations above)
21497 and both P and A were reference types (before being replaced with
21498 the type referred to above):
21499 - if the type from the argument template was an lvalue reference and
21500 the type from the parameter template was not, the argument type is
21501 considered to be more specialized than the other; otherwise,
21502 - if the type from the argument template is more cv-qualified
21503 than the type from the parameter template (as described above),
21504 the argument type is considered to be more specialized than the other;
21505 otherwise,
21506 - neither type is more specialized than the other." */
21507
21508 if (deduce1 && deduce2)
21509 {
21510 if (ref1 && ref2 && ref1 != ref2)
21511 {
21512 if (ref1 > ref2)
21513 lose1 = true;
21514 else
21515 lose2 = true;
21516 }
21517 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21518 {
21519 if ((quals1 & quals2) == quals2)
21520 lose2 = true;
21521 if ((quals1 & quals2) == quals1)
21522 lose1 = true;
21523 }
21524 }
21525
21526 if (lose1 && lose2)
21527 /* We've failed to deduce something in either direction.
21528 These must be unordered. */
21529 break;
21530
21531 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21532 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21533 /* We have already processed all of the arguments in our
21534 handing of the pack expansion type. */
21535 len = 0;
21536
21537 args1 = TREE_CHAIN (args1);
21538 args2 = TREE_CHAIN (args2);
21539 }
21540
21541 /* "In most cases, all template parameters must have values in order for
21542 deduction to succeed, but for partial ordering purposes a template
21543 parameter may remain without a value provided it is not used in the
21544 types being used for partial ordering."
21545
21546 Thus, if we are missing any of the targs1 we need to substitute into
21547 origs1, then pat2 is not as specialized as pat1. This can happen when
21548 there is a nondeduced context. */
21549 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21550 lose2 = true;
21551 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21552 lose1 = true;
21553
21554 processing_template_decl--;
21555
21556 /* If both deductions succeed, the partial ordering selects the more
21557 constrained template. */
21558 if (!lose1 && !lose2)
21559 {
21560 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21561 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21562 lose1 = !subsumes_constraints (c1, c2);
21563 lose2 = !subsumes_constraints (c2, c1);
21564 }
21565
21566 /* All things being equal, if the next argument is a pack expansion
21567 for one function but not for the other, prefer the
21568 non-variadic function. FIXME this is bogus; see c++/41958. */
21569 if (lose1 == lose2
21570 && args1 && TREE_VALUE (args1)
21571 && args2 && TREE_VALUE (args2))
21572 {
21573 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21574 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21575 }
21576
21577 if (lose1 == lose2)
21578 return 0;
21579 else if (!lose1)
21580 return 1;
21581 else
21582 return -1;
21583}
21584
21585/* Determine which of two partial specializations of TMPL is more
21586 specialized.
21587
21588 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21589 to the first partial specialization. The TREE_PURPOSE is the
21590 innermost set of template parameters for the partial
21591 specialization. PAT2 is similar, but for the second template.
21592
21593 Return 1 if the first partial specialization is more specialized;
21594 -1 if the second is more specialized; 0 if neither is more
21595 specialized.
21596
21597 See [temp.class.order] for information about determining which of
21598 two templates is more specialized. */
21599
21600static int
21601more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21602{
21603 tree targs;
21604 int winner = 0;
21605 bool any_deductions = false;
21606
21607 tree tmpl1 = TREE_VALUE (pat1);
21608 tree tmpl2 = TREE_VALUE (pat2);
21609 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21610 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21611
21612 /* Just like what happens for functions, if we are ordering between
21613 different template specializations, we may encounter dependent
21614 types in the arguments, and we need our dependency check functions
21615 to behave correctly. */
21616 ++processing_template_decl;
21617 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21618 if (targs)
21619 {
21620 --winner;
21621 any_deductions = true;
21622 }
21623
21624 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21625 if (targs)
21626 {
21627 ++winner;
21628 any_deductions = true;
21629 }
21630 --processing_template_decl;
21631
21632 /* If both deductions succeed, the partial ordering selects the more
21633 constrained template. */
21634 if (!winner && any_deductions)
21635 return more_constrained (tmpl1, tmpl2);
21636
21637 /* In the case of a tie where at least one of the templates
21638 has a parameter pack at the end, the template with the most
21639 non-packed parameters wins. */
21640 if (winner == 0
21641 && any_deductions
21642 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21643 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21644 {
21645 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21646 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21647 int len1 = TREE_VEC_LENGTH (args1);
21648 int len2 = TREE_VEC_LENGTH (args2);
21649
21650 /* We don't count the pack expansion at the end. */
21651 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21652 --len1;
21653 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21654 --len2;
21655
21656 if (len1 > len2)
21657 return 1;
21658 else if (len1 < len2)
21659 return -1;
21660 }
21661
21662 return winner;
21663}
21664
21665/* Return the template arguments that will produce the function signature
21666 DECL from the function template FN, with the explicit template
21667 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21668 also match. Return NULL_TREE if no satisfactory arguments could be
21669 found. */
21670
21671static tree
21672get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21673{
21674 int ntparms = DECL_NTPARMS (fn);
21675 tree targs = make_tree_vec (ntparms);
21676 tree decl_type = TREE_TYPE (decl);
21677 tree decl_arg_types;
21678 tree *args;
21679 unsigned int nargs, ix;
21680 tree arg;
21681
21682 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21683
21684 /* Never do unification on the 'this' parameter. */
21685 decl_arg_types = skip_artificial_parms_for (decl,
21686 TYPE_ARG_TYPES (decl_type));
21687
21688 nargs = list_length (decl_arg_types);
21689 args = XALLOCAVEC (tree, nargs);
21690 for (arg = decl_arg_types, ix = 0;
21691 arg != NULL_TREE && arg != void_list_node;
21692 arg = TREE_CHAIN (arg), ++ix)
21693 args[ix] = TREE_VALUE (arg);
21694
21695 if (fn_type_unification (fn, explicit_args, targs,
21696 args, ix,
21697 (check_rettype || DECL_CONV_FN_P (fn)
21698 ? TREE_TYPE (decl_type) : NULL_TREE),
21699 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21700 /*decltype*/false)
21701 == error_mark_node)
21702 return NULL_TREE;
21703
21704 return targs;
21705}
21706
21707/* Return the innermost template arguments that, when applied to a partial
21708 specialization SPEC_TMPL of TMPL, yield the ARGS.
21709
21710 For example, suppose we have:
21711
21712 template <class T, class U> struct S {};
21713 template <class T> struct S<T*, int> {};
21714
21715 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21716 partial specialization and the ARGS will be {double*, int}. The resulting
21717 vector will be {double}, indicating that `T' is bound to `double'. */
21718
21719static tree
21720get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21721{
21722 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21723 tree spec_args
21724 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21725 int i, ntparms = TREE_VEC_LENGTH (tparms);
21726 tree deduced_args;
21727 tree innermost_deduced_args;
21728
21729 innermost_deduced_args = make_tree_vec (ntparms);
21730 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21731 {
21732 deduced_args = copy_node (args);
21733 SET_TMPL_ARGS_LEVEL (deduced_args,
21734 TMPL_ARGS_DEPTH (deduced_args),
21735 innermost_deduced_args);
21736 }
21737 else
21738 deduced_args = innermost_deduced_args;
21739
21740 bool tried_array_deduction = (cxx_dialect < cxx1z);
21741 again:
21742 if (unify (tparms, deduced_args,
21743 INNERMOST_TEMPLATE_ARGS (spec_args),
21744 INNERMOST_TEMPLATE_ARGS (args),
21745 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21746 return NULL_TREE;
21747
21748 for (i = 0; i < ntparms; ++i)
21749 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21750 {
21751 if (!tried_array_deduction)
21752 {
21753 try_array_deduction (tparms, innermost_deduced_args,
21754 INNERMOST_TEMPLATE_ARGS (spec_args));
21755 tried_array_deduction = true;
21756 if (TREE_VEC_ELT (innermost_deduced_args, i))
21757 goto again;
21758 }
21759 return NULL_TREE;
21760 }
21761
21762 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21763 if (!push_tinst_level (tinst))
21764 {
21765 excessive_deduction_depth = true;
21766 return NULL_TREE;
21767 }
21768
21769 /* Verify that nondeduced template arguments agree with the type
21770 obtained from argument deduction.
21771
21772 For example:
21773
21774 struct A { typedef int X; };
21775 template <class T, class U> struct C {};
21776 template <class T> struct C<T, typename T::X> {};
21777
21778 Then with the instantiation `C<A, int>', we can deduce that
21779 `T' is `A' but unify () does not check whether `typename T::X'
21780 is `int'. */
21781 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21782
21783 if (spec_args != error_mark_node)
21784 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21785 INNERMOST_TEMPLATE_ARGS (spec_args),
21786 tmpl, tf_none, false, false);
21787
21788 pop_tinst_level ();
21789
21790 if (spec_args == error_mark_node
21791 /* We only need to check the innermost arguments; the other
21792 arguments will always agree. */
21793 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21794 INNERMOST_TEMPLATE_ARGS (args)))
21795 return NULL_TREE;
21796
21797 /* Now that we have bindings for all of the template arguments,
21798 ensure that the arguments deduced for the template template
21799 parameters have compatible template parameter lists. See the use
21800 of template_template_parm_bindings_ok_p in fn_type_unification
21801 for more information. */
21802 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21803 return NULL_TREE;
21804
21805 return deduced_args;
21806}
21807
21808// Compare two function templates T1 and T2 by deducing bindings
21809// from one against the other. If both deductions succeed, compare
21810// constraints to see which is more constrained.
21811static int
21812more_specialized_inst (tree t1, tree t2)
21813{
21814 int fate = 0;
21815 int count = 0;
21816
21817 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21818 {
21819 --fate;
21820 ++count;
21821 }
21822
21823 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21824 {
21825 ++fate;
21826 ++count;
21827 }
21828
21829 // If both deductions succeed, then one may be more constrained.
21830 if (count == 2 && fate == 0)
21831 fate = more_constrained (t1, t2);
21832
21833 return fate;
21834}
21835
21836/* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21837 Return the TREE_LIST node with the most specialized template, if
21838 any. If there is no most specialized template, the error_mark_node
21839 is returned.
21840
21841 Note that this function does not look at, or modify, the
21842 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21843 returned is one of the elements of INSTANTIATIONS, callers may
21844 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21845 and retrieve it from the value returned. */
21846
21847tree
21848most_specialized_instantiation (tree templates)
21849{
21850 tree fn, champ;
21851
21852 ++processing_template_decl;
21853
21854 champ = templates;
21855 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21856 {
21857 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21858 if (fate == -1)
21859 champ = fn;
21860 else if (!fate)
21861 {
21862 /* Equally specialized, move to next function. If there
21863 is no next function, nothing's most specialized. */
21864 fn = TREE_CHAIN (fn);
21865 champ = fn;
21866 if (!fn)
21867 break;
21868 }
21869 }
21870
21871 if (champ)
21872 /* Now verify that champ is better than everything earlier in the
21873 instantiation list. */
21874 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21875 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21876 {
21877 champ = NULL_TREE;
21878 break;
21879 }
21880 }
21881
21882 processing_template_decl--;
21883
21884 if (!champ)
21885 return error_mark_node;
21886
21887 return champ;
21888}
21889
21890/* If DECL is a specialization of some template, return the most
21891 general such template. Otherwise, returns NULL_TREE.
21892
21893 For example, given:
21894
21895 template <class T> struct S { template <class U> void f(U); };
21896
21897 if TMPL is `template <class U> void S<int>::f(U)' this will return
21898 the full template. This function will not trace past partial
21899 specializations, however. For example, given in addition:
21900
21901 template <class T> struct S<T*> { template <class U> void f(U); };
21902
21903 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21904 `template <class T> template <class U> S<T*>::f(U)'. */
21905
21906tree
21907most_general_template (tree decl)
21908{
21909 if (TREE_CODE (decl) != TEMPLATE_DECL)
21910 {
21911 if (tree tinfo = get_template_info (decl))
21912 decl = TI_TEMPLATE (tinfo);
21913 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21914 template friend, or a FIELD_DECL for a capture pack. */
21915 if (TREE_CODE (decl) != TEMPLATE_DECL)
21916 return NULL_TREE;
21917 }
21918
21919 /* Look for more and more general templates. */
21920 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21921 {
21922 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21923 (See cp-tree.h for details.) */
21924 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21925 break;
21926
21927 if (CLASS_TYPE_P (TREE_TYPE (decl))
21928 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21929 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21930 break;
21931
21932 /* Stop if we run into an explicitly specialized class template. */
21933 if (!DECL_NAMESPACE_SCOPE_P (decl)
21934 && DECL_CONTEXT (decl)
21935 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21936 break;
21937
21938 decl = DECL_TI_TEMPLATE (decl);
21939 }
21940
21941 return decl;
21942}
21943
21944/* Return the most specialized of the template partial specializations
21945 which can produce TARGET, a specialization of some class or variable
21946 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21947 a TEMPLATE_DECL node corresponding to the partial specialization, while
21948 the TREE_PURPOSE is the set of template arguments that must be
21949 substituted into the template pattern in order to generate TARGET.
21950
21951 If the choice of partial specialization is ambiguous, a diagnostic
21952 is issued, and the error_mark_node is returned. If there are no
21953 partial specializations matching TARGET, then NULL_TREE is
21954 returned, indicating that the primary template should be used. */
21955
21956static tree
21957most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21958{
21959 tree list = NULL_TREE;
21960 tree t;
21961 tree champ;
21962 int fate;
21963 bool ambiguous_p;
21964 tree outer_args = NULL_TREE;
21965 tree tmpl, args;
21966
21967 if (TYPE_P (target))
21968 {
21969 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21970 tmpl = TI_TEMPLATE (tinfo);
21971 args = TI_ARGS (tinfo);
21972 }
21973 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21974 {
21975 tmpl = TREE_OPERAND (target, 0);
21976 args = TREE_OPERAND (target, 1);
21977 }
21978 else if (VAR_P (target))
21979 {
21980 tree tinfo = DECL_TEMPLATE_INFO (target);
21981 tmpl = TI_TEMPLATE (tinfo);
21982 args = TI_ARGS (tinfo);
21983 }
21984 else
21985 gcc_unreachable ();
21986
21987 tree main_tmpl = most_general_template (tmpl);
21988
21989 /* For determining which partial specialization to use, only the
21990 innermost args are interesting. */
21991 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21992 {
21993 outer_args = strip_innermost_template_args (args, 1);
21994 args = INNERMOST_TEMPLATE_ARGS (args);
21995 }
21996
21997 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21998 {
21999 tree spec_args;
22000 tree spec_tmpl = TREE_VALUE (t);
22001
22002 if (outer_args)
22003 {
22004 /* Substitute in the template args from the enclosing class. */
22005 ++processing_template_decl;
22006 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22007 --processing_template_decl;
22008 }
22009
22010 if (spec_tmpl == error_mark_node)
22011 return error_mark_node;
22012
22013 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22014 if (spec_args)
22015 {
22016 if (outer_args)
22017 spec_args = add_to_template_args (outer_args, spec_args);
22018
22019 /* Keep the candidate only if the constraints are satisfied,
22020 or if we're not compiling with concepts. */
22021 if (!flag_concepts
22022 || constraints_satisfied_p (spec_tmpl, spec_args))
22023 {
22024 list = tree_cons (spec_args, TREE_VALUE (t), list);
22025 TREE_TYPE (list) = TREE_TYPE (t);
22026 }
22027 }
22028 }
22029
22030 if (! list)
22031 return NULL_TREE;
22032
22033 ambiguous_p = false;
22034 t = list;
22035 champ = t;
22036 t = TREE_CHAIN (t);
22037 for (; t; t = TREE_CHAIN (t))
22038 {
22039 fate = more_specialized_partial_spec (tmpl, champ, t);
22040 if (fate == 1)
22041 ;
22042 else
22043 {
22044 if (fate == 0)
22045 {
22046 t = TREE_CHAIN (t);
22047 if (! t)
22048 {
22049 ambiguous_p = true;
22050 break;
22051 }
22052 }
22053 champ = t;
22054 }
22055 }
22056
22057 if (!ambiguous_p)
22058 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22059 {
22060 fate = more_specialized_partial_spec (tmpl, champ, t);
22061 if (fate != 1)
22062 {
22063 ambiguous_p = true;
22064 break;
22065 }
22066 }
22067
22068 if (ambiguous_p)
22069 {
22070 const char *str;
22071 char *spaces = NULL;
22072 if (!(complain & tf_error))
22073 return error_mark_node;
22074 if (TYPE_P (target))
22075 error ("ambiguous template instantiation for %q#T", target);
22076 else
22077 error ("ambiguous template instantiation for %q#D", target);
22078 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22079 for (t = list; t; t = TREE_CHAIN (t))
22080 {
22081 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22082 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22083 "%s %#S", spaces ? spaces : str, subst);
22084 spaces = spaces ? spaces : get_spaces (str);
22085 }
22086 free (spaces);
22087 return error_mark_node;
22088 }
22089
22090 return champ;
22091}
22092
22093/* Explicitly instantiate DECL. */
22094
22095void
22096do_decl_instantiation (tree decl, tree storage)
22097{
22098 tree result = NULL_TREE;
22099 int extern_p = 0;
22100
22101 if (!decl || decl == error_mark_node)
22102 /* An error occurred, for which grokdeclarator has already issued
22103 an appropriate message. */
22104 return;
22105 else if (! DECL_LANG_SPECIFIC (decl))
22106 {
22107 error ("explicit instantiation of non-template %q#D", decl);
22108 return;
22109 }
22110
22111 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22112 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22113
22114 if (VAR_P (decl) && !var_templ)
22115 {
22116 /* There is an asymmetry here in the way VAR_DECLs and
22117 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22118 the latter, the DECL we get back will be marked as a
22119 template instantiation, and the appropriate
22120 DECL_TEMPLATE_INFO will be set up. This does not happen for
22121 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22122 should handle VAR_DECLs as it currently handles
22123 FUNCTION_DECLs. */
22124 if (!DECL_CLASS_SCOPE_P (decl))
22125 {
22126 error ("%qD is not a static data member of a class template", decl);
22127 return;
22128 }
22129 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22130 if (!result || !VAR_P (result))
22131 {
22132 error ("no matching template for %qD found", decl);
22133 return;
22134 }
22135 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22136 {
22137 error ("type %qT for explicit instantiation %qD does not match "
22138 "declared type %qT", TREE_TYPE (result), decl,
22139 TREE_TYPE (decl));
22140 return;
22141 }
22142 }
22143 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22144 {
22145 error ("explicit instantiation of %q#D", decl);
22146 return;
22147 }
22148 else
22149 result = decl;
22150
22151 /* Check for various error cases. Note that if the explicit
22152 instantiation is valid the RESULT will currently be marked as an
22153 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22154 until we get here. */
22155
22156 if (DECL_TEMPLATE_SPECIALIZATION (result))
22157 {
22158 /* DR 259 [temp.spec].
22159
22160 Both an explicit instantiation and a declaration of an explicit
22161 specialization shall not appear in a program unless the explicit
22162 instantiation follows a declaration of the explicit specialization.
22163
22164 For a given set of template parameters, if an explicit
22165 instantiation of a template appears after a declaration of an
22166 explicit specialization for that template, the explicit
22167 instantiation has no effect. */
22168 return;
22169 }
22170 else if (DECL_EXPLICIT_INSTANTIATION (result))
22171 {
22172 /* [temp.spec]
22173
22174 No program shall explicitly instantiate any template more
22175 than once.
22176
22177 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22178 the first instantiation was `extern' and the second is not,
22179 and EXTERN_P for the opposite case. */
22180 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22181 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22182 /* If an "extern" explicit instantiation follows an ordinary
22183 explicit instantiation, the template is instantiated. */
22184 if (extern_p)
22185 return;
22186 }
22187 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22188 {
22189 error ("no matching template for %qD found", result);
22190 return;
22191 }
22192 else if (!DECL_TEMPLATE_INFO (result))
22193 {
22194 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22195 return;
22196 }
22197
22198 if (storage == NULL_TREE)
22199 ;
22200 else if (storage == ridpointers[(int) RID_EXTERN])
22201 {
22202 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22203 pedwarn (input_location, OPT_Wpedantic,
22204 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22205 "instantiations");
22206 extern_p = 1;
22207 }
22208 else
22209 error ("storage class %qD applied to template instantiation", storage);
22210
22211 check_explicit_instantiation_namespace (result);
22212 mark_decl_instantiated (result, extern_p);
22213 if (! extern_p)
22214 instantiate_decl (result, /*defer_ok=*/true,
22215 /*expl_inst_class_mem_p=*/false);
22216}
22217
22218static void
22219mark_class_instantiated (tree t, int extern_p)
22220{
22221 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22222 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22223 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22224 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22225 if (! extern_p)
22226 {
22227 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22228 rest_of_type_compilation (t, 1);
22229 }
22230}
22231
22232/* Called from do_type_instantiation through binding_table_foreach to
22233 do recursive instantiation for the type bound in ENTRY. */
22234static void
22235bt_instantiate_type_proc (binding_entry entry, void *data)
22236{
22237 tree storage = *(tree *) data;
22238
22239 if (MAYBE_CLASS_TYPE_P (entry->type)
22240 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22241 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22242}
22243
22244/* Called from do_type_instantiation to instantiate a member
22245 (a member function or a static member variable) of an
22246 explicitly instantiated class template. */
22247static void
22248instantiate_class_member (tree decl, int extern_p)
22249{
22250 mark_decl_instantiated (decl, extern_p);
22251 if (! extern_p)
22252 instantiate_decl (decl, /*defer_ok=*/true,
22253 /*expl_inst_class_mem_p=*/true);
22254}
22255
22256/* Perform an explicit instantiation of template class T. STORAGE, if
22257 non-null, is the RID for extern, inline or static. COMPLAIN is
22258 nonzero if this is called from the parser, zero if called recursively,
22259 since the standard is unclear (as detailed below). */
22260
22261void
22262do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22263{
22264 int extern_p = 0;
22265 int nomem_p = 0;
22266 int static_p = 0;
22267 int previous_instantiation_extern_p = 0;
22268
22269 if (TREE_CODE (t) == TYPE_DECL)
22270 t = TREE_TYPE (t);
22271
22272 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22273 {
22274 tree tmpl =
22275 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22276 if (tmpl)
22277 error ("explicit instantiation of non-class template %qD", tmpl);
22278 else
22279 error ("explicit instantiation of non-template type %qT", t);
22280 return;
22281 }
22282
22283 complete_type (t);
22284
22285 if (!COMPLETE_TYPE_P (t))
22286 {
22287 if (complain & tf_error)
22288 error ("explicit instantiation of %q#T before definition of template",
22289 t);
22290 return;
22291 }
22292
22293 if (storage != NULL_TREE)
22294 {
22295 if (!in_system_header_at (input_location))
22296 {
22297 if (storage == ridpointers[(int) RID_EXTERN])
22298 {
22299 if (cxx_dialect == cxx98)
22300 pedwarn (input_location, OPT_Wpedantic,
22301 "ISO C++ 1998 forbids the use of %<extern%> on "
22302 "explicit instantiations");
22303 }
22304 else
22305 pedwarn (input_location, OPT_Wpedantic,
22306 "ISO C++ forbids the use of %qE"
22307 " on explicit instantiations", storage);
22308 }
22309
22310 if (storage == ridpointers[(int) RID_INLINE])
22311 nomem_p = 1;
22312 else if (storage == ridpointers[(int) RID_EXTERN])
22313 extern_p = 1;
22314 else if (storage == ridpointers[(int) RID_STATIC])
22315 static_p = 1;
22316 else
22317 {
22318 error ("storage class %qD applied to template instantiation",
22319 storage);
22320 extern_p = 0;
22321 }
22322 }
22323
22324 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22325 {
22326 /* DR 259 [temp.spec].
22327
22328 Both an explicit instantiation and a declaration of an explicit
22329 specialization shall not appear in a program unless the explicit
22330 instantiation follows a declaration of the explicit specialization.
22331
22332 For a given set of template parameters, if an explicit
22333 instantiation of a template appears after a declaration of an
22334 explicit specialization for that template, the explicit
22335 instantiation has no effect. */
22336 return;
22337 }
22338 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22339 {
22340 /* [temp.spec]
22341
22342 No program shall explicitly instantiate any template more
22343 than once.
22344
22345 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22346 instantiation was `extern'. If EXTERN_P then the second is.
22347 These cases are OK. */
22348 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22349
22350 if (!previous_instantiation_extern_p && !extern_p
22351 && (complain & tf_error))
22352 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22353
22354 /* If we've already instantiated the template, just return now. */
22355 if (!CLASSTYPE_INTERFACE_ONLY (t))
22356 return;
22357 }
22358
22359 check_explicit_instantiation_namespace (TYPE_NAME (t));
22360 mark_class_instantiated (t, extern_p);
22361
22362 if (nomem_p)
22363 return;
22364
22365 {
22366 tree tmp;
22367
22368 /* In contrast to implicit instantiation, where only the
22369 declarations, and not the definitions, of members are
22370 instantiated, we have here:
22371
22372 [temp.explicit]
22373
22374 The explicit instantiation of a class template specialization
22375 implies the instantiation of all of its members not
22376 previously explicitly specialized in the translation unit
22377 containing the explicit instantiation.
22378
22379 Of course, we can't instantiate member template classes, since
22380 we don't have any arguments for them. Note that the standard
22381 is unclear on whether the instantiation of the members are
22382 *explicit* instantiations or not. However, the most natural
22383 interpretation is that it should be an explicit instantiation. */
22384
22385 if (! static_p)
22386 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22387 if (TREE_CODE (tmp) == FUNCTION_DECL
22388 && DECL_TEMPLATE_INSTANTIATION (tmp)
22389 && user_provided_p (tmp))
22390 instantiate_class_member (tmp, extern_p);
22391
22392 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22393 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22394 instantiate_class_member (tmp, extern_p);
22395
22396 if (CLASSTYPE_NESTED_UTDS (t))
22397 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22398 bt_instantiate_type_proc, &storage);
22399 }
22400}
22401
22402/* Given a function DECL, which is a specialization of TMPL, modify
22403 DECL to be a re-instantiation of TMPL with the same template
22404 arguments. TMPL should be the template into which tsubst'ing
22405 should occur for DECL, not the most general template.
22406
22407 One reason for doing this is a scenario like this:
22408
22409 template <class T>
22410 void f(const T&, int i);
22411
22412 void g() { f(3, 7); }
22413
22414 template <class T>
22415 void f(const T& t, const int i) { }
22416
22417 Note that when the template is first instantiated, with
22418 instantiate_template, the resulting DECL will have no name for the
22419 first parameter, and the wrong type for the second. So, when we go
22420 to instantiate the DECL, we regenerate it. */
22421
22422static void
22423regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22424{
22425 /* The arguments used to instantiate DECL, from the most general
22426 template. */
22427 tree code_pattern;
22428
22429 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22430
22431 /* Make sure that we can see identifiers, and compute access
22432 correctly. */
22433 push_access_scope (decl);
22434
22435 if (TREE_CODE (decl) == FUNCTION_DECL)
22436 {
22437 tree decl_parm;
22438 tree pattern_parm;
22439 tree specs;
22440 int args_depth;
22441 int parms_depth;
22442
22443 args_depth = TMPL_ARGS_DEPTH (args);
22444 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22445 if (args_depth > parms_depth)
22446 args = get_innermost_template_args (args, parms_depth);
22447
22448 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22449 args, tf_error, NULL_TREE,
22450 /*defer_ok*/false);
22451 if (specs && specs != error_mark_node)
22452 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22453 specs);
22454
22455 /* Merge parameter declarations. */
22456 decl_parm = skip_artificial_parms_for (decl,
22457 DECL_ARGUMENTS (decl));
22458 pattern_parm
22459 = skip_artificial_parms_for (code_pattern,
22460 DECL_ARGUMENTS (code_pattern));
22461 while (decl_parm && !DECL_PACK_P (pattern_parm))
22462 {
22463 tree parm_type;
22464 tree attributes;
22465
22466 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22467 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22468 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22469 NULL_TREE);
22470 parm_type = type_decays_to (parm_type);
22471 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22472 TREE_TYPE (decl_parm) = parm_type;
22473 attributes = DECL_ATTRIBUTES (pattern_parm);
22474 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22475 {
22476 DECL_ATTRIBUTES (decl_parm) = attributes;
22477 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22478 }
22479 decl_parm = DECL_CHAIN (decl_parm);
22480 pattern_parm = DECL_CHAIN (pattern_parm);
22481 }
22482 /* Merge any parameters that match with the function parameter
22483 pack. */
22484 if (pattern_parm && DECL_PACK_P (pattern_parm))
22485 {
22486 int i, len;
22487 tree expanded_types;
22488 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22489 the parameters in this function parameter pack. */
22490 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22491 args, tf_error, NULL_TREE);
22492 len = TREE_VEC_LENGTH (expanded_types);
22493 for (i = 0; i < len; i++)
22494 {
22495 tree parm_type;
22496 tree attributes;
22497
22498 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22499 /* Rename the parameter to include the index. */
22500 DECL_NAME (decl_parm) =
22501 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22502 parm_type = TREE_VEC_ELT (expanded_types, i);
22503 parm_type = type_decays_to (parm_type);
22504 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22505 TREE_TYPE (decl_parm) = parm_type;
22506 attributes = DECL_ATTRIBUTES (pattern_parm);
22507 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22508 {
22509 DECL_ATTRIBUTES (decl_parm) = attributes;
22510 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22511 }
22512 decl_parm = DECL_CHAIN (decl_parm);
22513 }
22514 }
22515 /* Merge additional specifiers from the CODE_PATTERN. */
22516 if (DECL_DECLARED_INLINE_P (code_pattern)
22517 && !DECL_DECLARED_INLINE_P (decl))
22518 DECL_DECLARED_INLINE_P (decl) = 1;
22519 }
22520 else if (VAR_P (decl))
22521 {
22522 DECL_INITIAL (decl) =
22523 tsubst_expr (DECL_INITIAL (code_pattern), args,
22524 tf_error, DECL_TI_TEMPLATE (decl),
22525 /*integral_constant_expression_p=*/false);
22526 if (VAR_HAD_UNKNOWN_BOUND (decl))
22527 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22528 tf_error, DECL_TI_TEMPLATE (decl));
22529 }
22530 else
22531 gcc_unreachable ();
22532
22533 pop_access_scope (decl);
22534}
22535
22536/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22537 substituted to get DECL. */
22538
22539tree
22540template_for_substitution (tree decl)
22541{
22542 tree tmpl = DECL_TI_TEMPLATE (decl);
22543
22544 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22545 for the instantiation. This is not always the most general
22546 template. Consider, for example:
22547
22548 template <class T>
22549 struct S { template <class U> void f();
22550 template <> void f<int>(); };
22551
22552 and an instantiation of S<double>::f<int>. We want TD to be the
22553 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22554 while (/* An instantiation cannot have a definition, so we need a
22555 more general template. */
22556 DECL_TEMPLATE_INSTANTIATION (tmpl)
22557 /* We must also deal with friend templates. Given:
22558
22559 template <class T> struct S {
22560 template <class U> friend void f() {};
22561 };
22562
22563 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22564 so far as the language is concerned, but that's still
22565 where we get the pattern for the instantiation from. On
22566 other hand, if the definition comes outside the class, say:
22567
22568 template <class T> struct S {
22569 template <class U> friend void f();
22570 };
22571 template <class U> friend void f() {}
22572
22573 we don't need to look any further. That's what the check for
22574 DECL_INITIAL is for. */
22575 || (TREE_CODE (decl) == FUNCTION_DECL
22576 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22577 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22578 {
22579 /* The present template, TD, should not be a definition. If it
22580 were a definition, we should be using it! Note that we
22581 cannot restructure the loop to just keep going until we find
22582 a template with a definition, since that might go too far if
22583 a specialization was declared, but not defined. */
22584
22585 /* Fetch the more general template. */
22586 tmpl = DECL_TI_TEMPLATE (tmpl);
22587 }
22588
22589 return tmpl;
22590}
22591
22592/* Returns true if we need to instantiate this template instance even if we
22593 know we aren't going to emit it. */
22594
22595bool
22596always_instantiate_p (tree decl)
22597{
22598 /* We always instantiate inline functions so that we can inline them. An
22599 explicit instantiation declaration prohibits implicit instantiation of
22600 non-inline functions. With high levels of optimization, we would
22601 normally inline non-inline functions -- but we're not allowed to do
22602 that for "extern template" functions. Therefore, we check
22603 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22604 return ((TREE_CODE (decl) == FUNCTION_DECL
22605 && (DECL_DECLARED_INLINE_P (decl)
22606 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22607 /* And we need to instantiate static data members so that
22608 their initializers are available in integral constant
22609 expressions. */
22610 || (VAR_P (decl)
22611 && decl_maybe_constant_var_p (decl)));
22612}
22613
22614/* If FN has a noexcept-specifier that hasn't been instantiated yet,
22615 instantiate it now, modifying TREE_TYPE (fn). */
22616
22617void
22618maybe_instantiate_noexcept (tree fn)
22619{
22620 tree fntype, spec, noex, clone;
22621
22622 /* Don't instantiate a noexcept-specification from template context. */
22623 if (processing_template_decl)
22624 return;
22625
22626 if (DECL_CLONED_FUNCTION_P (fn))
22627 fn = DECL_CLONED_FUNCTION (fn);
22628 fntype = TREE_TYPE (fn);
22629 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22630
22631 if (!spec || !TREE_PURPOSE (spec))
22632 return;
22633
22634 noex = TREE_PURPOSE (spec);
22635
22636 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22637 {
22638 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22639 spec = get_defaulted_eh_spec (fn);
22640 else if (push_tinst_level (fn))
22641 {
22642 push_access_scope (fn);
22643 push_deferring_access_checks (dk_no_deferred);
22644 input_location = DECL_SOURCE_LOCATION (fn);
22645 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22646 DEFERRED_NOEXCEPT_ARGS (noex),
22647 tf_warning_or_error, fn,
22648 /*function_p=*/false,
22649 /*integral_constant_expression_p=*/true);
22650 pop_deferring_access_checks ();
22651 pop_access_scope (fn);
22652 pop_tinst_level ();
22653 spec = build_noexcept_spec (noex, tf_warning_or_error);
22654 if (spec == error_mark_node)
22655 spec = noexcept_false_spec;
22656 }
22657 else
22658 spec = noexcept_false_spec;
22659
22660 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22661 }
22662
22663 FOR_EACH_CLONE (clone, fn)
22664 {
22665 if (TREE_TYPE (clone) == fntype)
22666 TREE_TYPE (clone) = TREE_TYPE (fn);
22667 else
22668 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22669 }
22670}
22671
22672/* Produce the definition of D, a _DECL generated from a template. If
22673 DEFER_OK is true, then we don't have to actually do the
22674 instantiation now; we just have to do it sometime. Normally it is
22675 an error if this is an explicit instantiation but D is undefined.
22676 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22677 instantiated class template. */
22678
22679tree
22680instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22681{
22682 tree tmpl = DECL_TI_TEMPLATE (d);
22683 tree gen_args;
22684 tree args;
22685 tree td;
22686 tree code_pattern;
22687 tree spec;
22688 tree gen_tmpl;
22689 bool pattern_defined;
22690 location_t saved_loc = input_location;
22691 int saved_unevaluated_operand = cp_unevaluated_operand;
22692 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22693 bool external_p;
22694 bool deleted_p;
22695
22696 /* This function should only be used to instantiate templates for
22697 functions and static member variables. */
22698 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22699
22700 /* A concept is never instantiated. */
22701 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22702
22703 /* Variables are never deferred; if instantiation is required, they
22704 are instantiated right away. That allows for better code in the
22705 case that an expression refers to the value of the variable --
22706 if the variable has a constant value the referring expression can
22707 take advantage of that fact. */
22708 if (VAR_P (d))
22709 defer_ok = false;
22710
22711 /* Don't instantiate cloned functions. Instead, instantiate the
22712 functions they cloned. */
22713 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22714 d = DECL_CLONED_FUNCTION (d);
22715
22716 if (DECL_TEMPLATE_INSTANTIATED (d)
22717 || (TREE_CODE (d) == FUNCTION_DECL
22718 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22719 || DECL_TEMPLATE_SPECIALIZATION (d))
22720 /* D has already been instantiated or explicitly specialized, so
22721 there's nothing for us to do here.
22722
22723 It might seem reasonable to check whether or not D is an explicit
22724 instantiation, and, if so, stop here. But when an explicit
22725 instantiation is deferred until the end of the compilation,
22726 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22727 the instantiation. */
22728 return d;
22729
22730 /* Check to see whether we know that this template will be
22731 instantiated in some other file, as with "extern template"
22732 extension. */
22733 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22734
22735 /* In general, we do not instantiate such templates. */
22736 if (external_p && !always_instantiate_p (d))
22737 return d;
22738
22739 gen_tmpl = most_general_template (tmpl);
22740 gen_args = DECL_TI_ARGS (d);
22741
22742 if (tmpl != gen_tmpl)
22743 /* We should already have the extra args. */
22744 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22745 == TMPL_ARGS_DEPTH (gen_args));
22746 /* And what's in the hash table should match D. */
22747 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22748 || spec == NULL_TREE);
22749
22750 /* This needs to happen before any tsubsting. */
22751 if (! push_tinst_level (d))
22752 return d;
22753
22754 timevar_push (TV_TEMPLATE_INST);
22755
22756 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22757 for the instantiation. */
22758 td = template_for_substitution (d);
22759 args = gen_args;
22760
22761 if (VAR_P (d))
22762 {
22763 /* Look up an explicit specialization, if any. */
22764 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22765 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22766 if (elt && elt != error_mark_node)
22767 {
22768 td = TREE_VALUE (elt);
22769 args = TREE_PURPOSE (elt);
22770 }
22771 }
22772
22773 code_pattern = DECL_TEMPLATE_RESULT (td);
22774
22775 /* We should never be trying to instantiate a member of a class
22776 template or partial specialization. */
22777 gcc_assert (d != code_pattern);
22778
22779 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22780 || DECL_TEMPLATE_SPECIALIZATION (td))
22781 /* In the case of a friend template whose definition is provided
22782 outside the class, we may have too many arguments. Drop the
22783 ones we don't need. The same is true for specializations. */
22784 args = get_innermost_template_args
22785 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22786
22787 if (TREE_CODE (d) == FUNCTION_DECL)
22788 {
22789 deleted_p = DECL_DELETED_FN (code_pattern);
22790 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22791 && DECL_INITIAL (code_pattern) != error_mark_node)
22792 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22793 || deleted_p);
22794 }
22795 else
22796 {
22797 deleted_p = false;
22798 if (DECL_CLASS_SCOPE_P (code_pattern))
22799 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22800 || DECL_INLINE_VAR_P (code_pattern));
22801 else
22802 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22803 }
22804
22805 /* We may be in the middle of deferred access check. Disable it now. */
22806 push_deferring_access_checks (dk_no_deferred);
22807
22808 /* Unless an explicit instantiation directive has already determined
22809 the linkage of D, remember that a definition is available for
22810 this entity. */
22811 if (pattern_defined
22812 && !DECL_INTERFACE_KNOWN (d)
22813 && !DECL_NOT_REALLY_EXTERN (d))
22814 mark_definable (d);
22815
22816 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22817 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22818 input_location = DECL_SOURCE_LOCATION (d);
22819
22820 /* If D is a member of an explicitly instantiated class template,
22821 and no definition is available, treat it like an implicit
22822 instantiation. */
22823 if (!pattern_defined && expl_inst_class_mem_p
22824 && DECL_EXPLICIT_INSTANTIATION (d))
22825 {
22826 /* Leave linkage flags alone on instantiations with anonymous
22827 visibility. */
22828 if (TREE_PUBLIC (d))
22829 {
22830 DECL_NOT_REALLY_EXTERN (d) = 0;
22831 DECL_INTERFACE_KNOWN (d) = 0;
22832 }
22833 SET_DECL_IMPLICIT_INSTANTIATION (d);
22834 }
22835
22836 /* Defer all other templates, unless we have been explicitly
22837 forbidden from doing so. */
22838 if (/* If there is no definition, we cannot instantiate the
22839 template. */
22840 ! pattern_defined
22841 /* If it's OK to postpone instantiation, do so. */
22842 || defer_ok
22843 /* If this is a static data member that will be defined
22844 elsewhere, we don't want to instantiate the entire data
22845 member, but we do want to instantiate the initializer so that
22846 we can substitute that elsewhere. */
22847 || (external_p && VAR_P (d))
22848 /* Handle here a deleted function too, avoid generating
22849 its body (c++/61080). */
22850 || deleted_p)
22851 {
22852 /* The definition of the static data member is now required so
22853 we must substitute the initializer. */
22854 if (VAR_P (d)
22855 && !DECL_INITIAL (d)
22856 && DECL_INITIAL (code_pattern))
22857 {
22858 tree ns;
22859 tree init;
22860 bool const_init = false;
22861 bool enter_context = DECL_CLASS_SCOPE_P (d);
22862
22863 ns = decl_namespace_context (d);
22864 push_nested_namespace (ns);
22865 if (enter_context)
22866 push_nested_class (DECL_CONTEXT (d));
22867 init = tsubst_expr (DECL_INITIAL (code_pattern),
22868 args,
22869 tf_warning_or_error, NULL_TREE,
22870 /*integral_constant_expression_p=*/false);
22871 /* If instantiating the initializer involved instantiating this
22872 again, don't call cp_finish_decl twice. */
22873 if (!DECL_INITIAL (d))
22874 {
22875 /* Make sure the initializer is still constant, in case of
22876 circular dependency (template/instantiate6.C). */
22877 const_init
22878 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22879 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22880 /*asmspec_tree=*/NULL_TREE,
22881 LOOKUP_ONLYCONVERTING);
22882 }
22883 if (enter_context)
22884 pop_nested_class ();
22885 pop_nested_namespace (ns);
22886 }
22887
22888 /* We restore the source position here because it's used by
22889 add_pending_template. */
22890 input_location = saved_loc;
22891
22892 if (at_eof && !pattern_defined
22893 && DECL_EXPLICIT_INSTANTIATION (d)
22894 && DECL_NOT_REALLY_EXTERN (d))
22895 /* [temp.explicit]
22896
22897 The definition of a non-exported function template, a
22898 non-exported member function template, or a non-exported
22899 member function or static data member of a class template
22900 shall be present in every translation unit in which it is
22901 explicitly instantiated. */
22902 permerror (input_location, "explicit instantiation of %qD "
22903 "but no definition available", d);
22904
22905 /* If we're in unevaluated context, we just wanted to get the
22906 constant value; this isn't an odr use, so don't queue
22907 a full instantiation. */
22908 if (cp_unevaluated_operand != 0)
22909 goto out;
22910 /* ??? Historically, we have instantiated inline functions, even
22911 when marked as "extern template". */
22912 if (!(external_p && VAR_P (d)))
22913 add_pending_template (d);
22914 goto out;
22915 }
22916 /* Tell the repository that D is available in this translation unit
22917 -- and see if it is supposed to be instantiated here. */
22918 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22919 {
22920 /* In a PCH file, despite the fact that the repository hasn't
22921 requested instantiation in the PCH it is still possible that
22922 an instantiation will be required in a file that includes the
22923 PCH. */
22924 if (pch_file)
22925 add_pending_template (d);
22926 /* Instantiate inline functions so that the inliner can do its
22927 job, even though we'll not be emitting a copy of this
22928 function. */
22929 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22930 goto out;
22931 }
22932
22933 bool push_to_top, nested;
22934 tree fn_context;
22935 fn_context = decl_function_context (d);
22936 nested = current_function_decl != NULL_TREE;
22937 push_to_top = !(nested && fn_context == current_function_decl);
22938
22939 vec<tree> omp_privatization_save;
22940 if (nested)
22941 save_omp_privatization_clauses (omp_privatization_save);
22942
22943 if (push_to_top)
22944 push_to_top_level ();
22945 else
22946 {
22947 push_function_context ();
22948 cp_unevaluated_operand = 0;
22949 c_inhibit_evaluation_warnings = 0;
22950 }
22951
22952 /* Mark D as instantiated so that recursive calls to
22953 instantiate_decl do not try to instantiate it again. */
22954 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22955
22956 /* Regenerate the declaration in case the template has been modified
22957 by a subsequent redeclaration. */
22958 regenerate_decl_from_template (d, td, args);
22959
22960 /* We already set the file and line above. Reset them now in case
22961 they changed as a result of calling regenerate_decl_from_template. */
22962 input_location = DECL_SOURCE_LOCATION (d);
22963
22964 if (VAR_P (d))
22965 {
22966 tree init;
22967 bool const_init = false;
22968
22969 /* Clear out DECL_RTL; whatever was there before may not be right
22970 since we've reset the type of the declaration. */
22971 SET_DECL_RTL (d, NULL);
22972 DECL_IN_AGGR_P (d) = 0;
22973
22974 /* The initializer is placed in DECL_INITIAL by
22975 regenerate_decl_from_template so we don't need to
22976 push/pop_access_scope again here. Pull it out so that
22977 cp_finish_decl can process it. */
22978 init = DECL_INITIAL (d);
22979 DECL_INITIAL (d) = NULL_TREE;
22980 DECL_INITIALIZED_P (d) = 0;
22981
22982 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22983 initializer. That function will defer actual emission until
22984 we have a chance to determine linkage. */
22985 DECL_EXTERNAL (d) = 0;
22986
22987 /* Enter the scope of D so that access-checking works correctly. */
22988 bool enter_context = DECL_CLASS_SCOPE_P (d);
22989 if (enter_context)
22990 push_nested_class (DECL_CONTEXT (d));
22991
22992 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22993 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22994
22995 if (enter_context)
22996 pop_nested_class ();
22997
22998 if (variable_template_p (gen_tmpl))
22999 note_variable_template_instantiation (d);
23000 }
23001 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23002 synthesize_method (d);
23003 else if (TREE_CODE (d) == FUNCTION_DECL)
23004 {
23005 hash_map<tree, tree> *saved_local_specializations;
23006 tree tmpl_parm;
23007 tree spec_parm;
23008 tree block = NULL_TREE;
23009 tree lambda_ctx = NULL_TREE;
23010
23011 /* Save away the current list, in case we are instantiating one
23012 template from within the body of another. */
23013 saved_local_specializations = local_specializations;
23014
23015 /* Set up the list of local specializations. */
23016 local_specializations = new hash_map<tree, tree>;
23017
23018 /* Set up context. */
23019 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23020 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23021 block = push_stmt_list ();
23022 else
23023 {
23024 if (push_to_top && LAMBDA_FUNCTION_P (d))
23025 {
23026 /* When instantiating a lambda's templated function
23027 operator, we need to push the non-lambda class scope
23028 of the lambda itself so that the nested function
23029 stack is sufficiently correct to deal with this
23030 capture. */
23031 lambda_ctx = DECL_CONTEXT (d);
23032 do
23033 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
23034 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
23035 if (lambda_ctx)
23036 push_nested_class (lambda_ctx);
23037 }
23038 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23039 }
23040
23041 /* Some typedefs referenced from within the template code need to be
23042 access checked at template instantiation time, i.e now. These
23043 types were added to the template at parsing time. Let's get those
23044 and perform the access checks then. */
23045 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23046 args);
23047
23048 /* Create substitution entries for the parameters. */
23049 tmpl_parm = DECL_ARGUMENTS (code_pattern);
23050 spec_parm = DECL_ARGUMENTS (d);
23051 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
23052 {
23053 register_local_specialization (spec_parm, tmpl_parm);
23054 spec_parm = skip_artificial_parms_for (d, spec_parm);
23055 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
23056 }
23057 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23058 {
23059 if (!DECL_PACK_P (tmpl_parm))
23060 {
23061 register_local_specialization (spec_parm, tmpl_parm);
23062 spec_parm = DECL_CHAIN (spec_parm);
23063 }
23064 else
23065 {
23066 /* Register the (value) argument pack as a specialization of
23067 TMPL_PARM, then move on. */
23068 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23069 register_local_specialization (argpack, tmpl_parm);
23070 }
23071 }
23072 gcc_assert (!spec_parm);
23073
23074 /* Substitute into the body of the function. */
23075 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23076 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23077 tf_warning_or_error, tmpl);
23078 else
23079 {
23080 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23081 tf_warning_or_error, tmpl,
23082 /*integral_constant_expression_p=*/false);
23083
23084 /* Set the current input_location to the end of the function
23085 so that finish_function knows where we are. */
23086 input_location
23087 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23088
23089 /* Remember if we saw an infinite loop in the template. */
23090 current_function_infinite_loop
23091 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23092 }
23093
23094 /* We don't need the local specializations any more. */
23095 delete local_specializations;
23096 local_specializations = saved_local_specializations;
23097
23098 /* Finish the function. */
23099 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23100 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23101 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23102 else
23103 {
23104 d = finish_function (0);
23105 expand_or_defer_fn (d);
23106 }
23107 if (lambda_ctx)
23108 pop_nested_class ();
23109
23110 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23111 cp_check_omp_declare_reduction (d);
23112 }
23113
23114 /* We're not deferring instantiation any more. */
23115 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23116
23117 if (push_to_top)
23118 pop_from_top_level ();
23119 else
23120 pop_function_context ();
23121
23122 if (nested)
23123 restore_omp_privatization_clauses (omp_privatization_save);
23124
23125out:
23126 pop_deferring_access_checks ();
23127 timevar_pop (TV_TEMPLATE_INST);
23128 pop_tinst_level ();
23129 input_location = saved_loc;
23130 cp_unevaluated_operand = saved_unevaluated_operand;
23131 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23132
23133 return d;
23134}
23135
23136/* Run through the list of templates that we wish we could
23137 instantiate, and instantiate any we can. RETRIES is the
23138 number of times we retry pending template instantiation. */
23139
23140void
23141instantiate_pending_templates (int retries)
23142{
23143 int reconsider;
23144 location_t saved_loc = input_location;
23145
23146 /* Instantiating templates may trigger vtable generation. This in turn
23147 may require further template instantiations. We place a limit here
23148 to avoid infinite loop. */
23149 if (pending_templates && retries >= max_tinst_depth)
23150 {
23151 tree decl = pending_templates->tinst->decl;
23152
23153 fatal_error (input_location,
23154 "template instantiation depth exceeds maximum of %d"
23155 " instantiating %q+D, possibly from virtual table generation"
23156 " (use -ftemplate-depth= to increase the maximum)",
23157 max_tinst_depth, decl);
23158 if (TREE_CODE (decl) == FUNCTION_DECL)
23159 /* Pretend that we defined it. */
23160 DECL_INITIAL (decl) = error_mark_node;
23161 return;
23162 }
23163
23164 do
23165 {
23166 struct pending_template **t = &pending_templates;
23167 struct pending_template *last = NULL;
23168 reconsider = 0;
23169 while (*t)
23170 {
23171 tree instantiation = reopen_tinst_level ((*t)->tinst);
23172 bool complete = false;
23173
23174 if (TYPE_P (instantiation))
23175 {
23176 tree fn;
23177
23178 if (!COMPLETE_TYPE_P (instantiation))
23179 {
23180 instantiate_class_template (instantiation);
23181 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23182 for (fn = TYPE_METHODS (instantiation);
23183 fn;
23184 fn = TREE_CHAIN (fn))
23185 if (! DECL_ARTIFICIAL (fn))
23186 instantiate_decl (fn,
23187 /*defer_ok=*/false,
23188 /*expl_inst_class_mem_p=*/false);
23189 if (COMPLETE_TYPE_P (instantiation))
23190 reconsider = 1;
23191 }
23192
23193 complete = COMPLETE_TYPE_P (instantiation);
23194 }
23195 else
23196 {
23197 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23198 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23199 {
23200 instantiation
23201 = instantiate_decl (instantiation,
23202 /*defer_ok=*/false,
23203 /*expl_inst_class_mem_p=*/false);
23204 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23205 reconsider = 1;
23206 }
23207
23208 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23209 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23210 }
23211
23212 if (complete)
23213 /* If INSTANTIATION has been instantiated, then we don't
23214 need to consider it again in the future. */
23215 *t = (*t)->next;
23216 else
23217 {
23218 last = *t;
23219 t = &(*t)->next;
23220 }
23221 tinst_depth = 0;
23222 current_tinst_level = NULL;
23223 }
23224 last_pending_template = last;
23225 }
23226 while (reconsider);
23227
23228 input_location = saved_loc;
23229}
23230
23231/* Substitute ARGVEC into T, which is a list of initializers for
23232 either base class or a non-static data member. The TREE_PURPOSEs
23233 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23234 instantiate_decl. */
23235
23236static tree
23237tsubst_initializer_list (tree t, tree argvec)
23238{
23239 tree inits = NULL_TREE;
23240
23241 for (; t; t = TREE_CHAIN (t))
23242 {
23243 tree decl;
23244 tree init;
23245 tree expanded_bases = NULL_TREE;
23246 tree expanded_arguments = NULL_TREE;
23247 int i, len = 1;
23248
23249 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23250 {
23251 tree expr;
23252 tree arg;
23253
23254 /* Expand the base class expansion type into separate base
23255 classes. */
23256 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23257 tf_warning_or_error,
23258 NULL_TREE);
23259 if (expanded_bases == error_mark_node)
23260 continue;
23261
23262 /* We'll be building separate TREE_LISTs of arguments for
23263 each base. */
23264 len = TREE_VEC_LENGTH (expanded_bases);
23265 expanded_arguments = make_tree_vec (len);
23266 for (i = 0; i < len; i++)
23267 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23268
23269 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23270 expand each argument in the TREE_VALUE of t. */
23271 expr = make_node (EXPR_PACK_EXPANSION);
23272 PACK_EXPANSION_LOCAL_P (expr) = true;
23273 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23274 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23275
23276 if (TREE_VALUE (t) == void_type_node)
23277 /* VOID_TYPE_NODE is used to indicate
23278 value-initialization. */
23279 {
23280 for (i = 0; i < len; i++)
23281 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23282 }
23283 else
23284 {
23285 /* Substitute parameter packs into each argument in the
23286 TREE_LIST. */
23287 in_base_initializer = 1;
23288 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23289 {
23290 tree expanded_exprs;
23291
23292 /* Expand the argument. */
23293 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23294 expanded_exprs
23295 = tsubst_pack_expansion (expr, argvec,
23296 tf_warning_or_error,
23297 NULL_TREE);
23298 if (expanded_exprs == error_mark_node)
23299 continue;
23300
23301 /* Prepend each of the expanded expressions to the
23302 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23303 for (i = 0; i < len; i++)
23304 {
23305 TREE_VEC_ELT (expanded_arguments, i) =
23306 tree_cons (NULL_TREE,
23307 TREE_VEC_ELT (expanded_exprs, i),
23308 TREE_VEC_ELT (expanded_arguments, i));
23309 }
23310 }
23311 in_base_initializer = 0;
23312
23313 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23314 since we built them backwards. */
23315 for (i = 0; i < len; i++)
23316 {
23317 TREE_VEC_ELT (expanded_arguments, i) =
23318 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23319 }
23320 }
23321 }
23322
23323 for (i = 0; i < len; ++i)
23324 {
23325 if (expanded_bases)
23326 {
23327 decl = TREE_VEC_ELT (expanded_bases, i);
23328 decl = expand_member_init (decl);
23329 init = TREE_VEC_ELT (expanded_arguments, i);
23330 }
23331 else
23332 {
23333 tree tmp;
23334 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23335 tf_warning_or_error, NULL_TREE);
23336
23337 decl = expand_member_init (decl);
23338 if (decl && !DECL_P (decl))
23339 in_base_initializer = 1;
23340
23341 init = TREE_VALUE (t);
23342 tmp = init;
23343 if (init != void_type_node)
23344 init = tsubst_expr (init, argvec,
23345 tf_warning_or_error, NULL_TREE,
23346 /*integral_constant_expression_p=*/false);
23347 if (init == NULL_TREE && tmp != NULL_TREE)
23348 /* If we had an initializer but it instantiated to nothing,
23349 value-initialize the object. This will only occur when
23350 the initializer was a pack expansion where the parameter
23351 packs used in that expansion were of length zero. */
23352 init = void_type_node;
23353 in_base_initializer = 0;
23354 }
23355
23356 if (decl)
23357 {
23358 init = build_tree_list (decl, init);
23359 TREE_CHAIN (init) = inits;
23360 inits = init;
23361 }
23362 }
23363 }
23364 return inits;
23365}
23366
23367/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23368
23369static void
23370set_current_access_from_decl (tree decl)
23371{
23372 if (TREE_PRIVATE (decl))
23373 current_access_specifier = access_private_node;
23374 else if (TREE_PROTECTED (decl))
23375 current_access_specifier = access_protected_node;
23376 else
23377 current_access_specifier = access_public_node;
23378}
23379
23380/* Instantiate an enumerated type. TAG is the template type, NEWTAG
23381 is the instantiation (which should have been created with
23382 start_enum) and ARGS are the template arguments to use. */
23383
23384static void
23385tsubst_enum (tree tag, tree newtag, tree args)
23386{
23387 tree e;
23388
23389 if (SCOPED_ENUM_P (newtag))
23390 begin_scope (sk_scoped_enum, newtag);
23391
23392 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23393 {
23394 tree value;
23395 tree decl;
23396
23397 decl = TREE_VALUE (e);
23398 /* Note that in a template enum, the TREE_VALUE is the
23399 CONST_DECL, not the corresponding INTEGER_CST. */
23400 value = tsubst_expr (DECL_INITIAL (decl),
23401 args, tf_warning_or_error, NULL_TREE,
23402 /*integral_constant_expression_p=*/true);
23403
23404 /* Give this enumeration constant the correct access. */
23405 set_current_access_from_decl (decl);
23406
23407 /* Actually build the enumerator itself. Here we're assuming that
23408 enumerators can't have dependent attributes. */
23409 build_enumerator (DECL_NAME (decl), value, newtag,
23410 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23411 }
23412
23413 if (SCOPED_ENUM_P (newtag))
23414 finish_scope ();
23415
23416 finish_enum_value_list (newtag);
23417 finish_enum (newtag);
23418
23419 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23420 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23421}
23422
23423/* DECL is a FUNCTION_DECL that is a template specialization. Return
23424 its type -- but without substituting the innermost set of template
23425 arguments. So, innermost set of template parameters will appear in
23426 the type. */
23427
23428tree
23429get_mostly_instantiated_function_type (tree decl)
23430{
23431 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23432 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23433}
23434
23435/* Return truthvalue if we're processing a template different from
23436 the last one involved in diagnostics. */
23437bool
23438problematic_instantiation_changed (void)
23439{
23440 return current_tinst_level != last_error_tinst_level;
23441}
23442
23443/* Remember current template involved in diagnostics. */
23444void
23445record_last_problematic_instantiation (void)
23446{
23447 last_error_tinst_level = current_tinst_level;
23448}
23449
23450struct tinst_level *
23451current_instantiation (void)
23452{
23453 return current_tinst_level;
23454}
23455
23456/* Return TRUE if current_function_decl is being instantiated, false
23457 otherwise. */
23458
23459bool
23460instantiating_current_function_p (void)
23461{
23462 return (current_instantiation ()
23463 && current_instantiation ()->decl == current_function_decl);
23464}
23465
23466/* [temp.param] Check that template non-type parm TYPE is of an allowable
23467 type. Return zero for ok, nonzero for disallowed. Issue error and
23468 warning messages under control of COMPLAIN. */
23469
23470static int
23471invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23472{
23473 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23474 return 0;
23475 else if (POINTER_TYPE_P (type))
23476 return 0;
23477 else if (TYPE_PTRMEM_P (type))
23478 return 0;
23479 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23480 return 0;
23481 else if (TREE_CODE (type) == TYPENAME_TYPE)
23482 return 0;
23483 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23484 return 0;
23485 else if (TREE_CODE (type) == NULLPTR_TYPE)
23486 return 0;
23487 /* A bound template template parm could later be instantiated to have a valid
23488 nontype parm type via an alias template. */
23489 else if (cxx_dialect >= cxx11
23490 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23491 return 0;
23492
23493 if (complain & tf_error)
23494 {
23495 if (type == error_mark_node)
23496 inform (input_location, "invalid template non-type parameter");
23497 else
23498 error ("%q#T is not a valid type for a template non-type parameter",
23499 type);
23500 }
23501 return 1;
23502}
23503
23504/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23505 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23506
23507static bool
23508dependent_type_p_r (tree type)
23509{
23510 tree scope;
23511
23512 /* [temp.dep.type]
23513
23514 A type is dependent if it is:
23515
23516 -- a template parameter. Template template parameters are types
23517 for us (since TYPE_P holds true for them) so we handle
23518 them here. */
23519 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23520 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23521 return true;
23522 /* -- a qualified-id with a nested-name-specifier which contains a
23523 class-name that names a dependent type or whose unqualified-id
23524 names a dependent type. */
23525 if (TREE_CODE (type) == TYPENAME_TYPE)
23526 return true;
23527
23528 /* An alias template specialization can be dependent even if the
23529 resulting type is not. */
23530 if (dependent_alias_template_spec_p (type))
23531 return true;
23532
23533 /* -- a cv-qualified type where the cv-unqualified type is
23534 dependent.
23535 No code is necessary for this bullet; the code below handles
23536 cv-qualified types, and we don't want to strip aliases with
23537 TYPE_MAIN_VARIANT because of DR 1558. */
23538 /* -- a compound type constructed from any dependent type. */
23539 if (TYPE_PTRMEM_P (type))
23540 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23541 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23542 (type)));
23543 else if (TYPE_PTR_P (type)
23544 || TREE_CODE (type) == REFERENCE_TYPE)
23545 return dependent_type_p (TREE_TYPE (type));
23546 else if (TREE_CODE (type) == FUNCTION_TYPE
23547 || TREE_CODE (type) == METHOD_TYPE)
23548 {
23549 tree arg_type;
23550
23551 if (dependent_type_p (TREE_TYPE (type)))
23552 return true;
23553 for (arg_type = TYPE_ARG_TYPES (type);
23554 arg_type;
23555 arg_type = TREE_CHAIN (arg_type))
23556 if (dependent_type_p (TREE_VALUE (arg_type)))
23557 return true;
23558 if (cxx_dialect >= cxx1z)
23559 {
23560 /* A value-dependent noexcept-specifier makes the type dependent. */
23561 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23562 if (spec && TREE_PURPOSE (spec)
23563 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23564 return true;
23565 }
23566 return false;
23567 }
23568 /* -- an array type constructed from any dependent type or whose
23569 size is specified by a constant expression that is
23570 value-dependent.
23571
23572 We checked for type- and value-dependence of the bounds in
23573 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23574 if (TREE_CODE (type) == ARRAY_TYPE)
23575 {
23576 if (TYPE_DOMAIN (type)
23577 && dependent_type_p (TYPE_DOMAIN (type)))
23578 return true;
23579 return dependent_type_p (TREE_TYPE (type));
23580 }
23581
23582 /* -- a template-id in which either the template name is a template
23583 parameter ... */
23584 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23585 return true;
23586 /* ... or any of the template arguments is a dependent type or
23587 an expression that is type-dependent or value-dependent. */
23588 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23589 && (any_dependent_template_arguments_p
23590 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23591 return true;
23592
23593 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23594 dependent; if the argument of the `typeof' expression is not
23595 type-dependent, then it should already been have resolved. */
23596 if (TREE_CODE (type) == TYPEOF_TYPE
23597 || TREE_CODE (type) == DECLTYPE_TYPE
23598 || TREE_CODE (type) == UNDERLYING_TYPE)
23599 return true;
23600
23601 /* A template argument pack is dependent if any of its packed
23602 arguments are. */
23603 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23604 {
23605 tree args = ARGUMENT_PACK_ARGS (type);
23606 int i, len = TREE_VEC_LENGTH (args);
23607 for (i = 0; i < len; ++i)
23608 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23609 return true;
23610 }
23611
23612 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23613 be template parameters. */
23614 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23615 return true;
23616
23617 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23618 return true;
23619
23620 /* The standard does not specifically mention types that are local
23621 to template functions or local classes, but they should be
23622 considered dependent too. For example:
23623
23624 template <int I> void f() {
23625 enum E { a = I };
23626 S<sizeof (E)> s;
23627 }
23628
23629 The size of `E' cannot be known until the value of `I' has been
23630 determined. Therefore, `E' must be considered dependent. */
23631 scope = TYPE_CONTEXT (type);
23632 if (scope && TYPE_P (scope))
23633 return dependent_type_p (scope);
23634 /* Don't use type_dependent_expression_p here, as it can lead
23635 to infinite recursion trying to determine whether a lambda
23636 nested in a lambda is dependent (c++/47687). */
23637 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23638 && DECL_LANG_SPECIFIC (scope)
23639 && DECL_TEMPLATE_INFO (scope)
23640 && (any_dependent_template_arguments_p
23641 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23642 return true;
23643
23644 /* Other types are non-dependent. */
23645 return false;
23646}
23647
23648/* Returns TRUE if TYPE is dependent, in the sense of
23649 [temp.dep.type]. Note that a NULL type is considered dependent. */
23650
23651bool
23652dependent_type_p (tree type)
23653{
23654 /* If there are no template parameters in scope, then there can't be
23655 any dependent types. */
23656 if (!processing_template_decl)
23657 {
23658 /* If we are not processing a template, then nobody should be
23659 providing us with a dependent type. */
23660 gcc_assert (type);
23661 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23662 return false;
23663 }
23664
23665 /* If the type is NULL, we have not computed a type for the entity
23666 in question; in that case, the type is dependent. */
23667 if (!type)
23668 return true;
23669
23670 /* Erroneous types can be considered non-dependent. */
23671 if (type == error_mark_node)
23672 return false;
23673
23674 /* If we have not already computed the appropriate value for TYPE,
23675 do so now. */
23676 if (!TYPE_DEPENDENT_P_VALID (type))
23677 {
23678 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23679 TYPE_DEPENDENT_P_VALID (type) = 1;
23680 }
23681
23682 return TYPE_DEPENDENT_P (type);
23683}
23684
23685/* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23686 lookup. In other words, a dependent type that is not the current
23687 instantiation. */
23688
23689bool
23690dependent_scope_p (tree scope)
23691{
23692 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23693 && !currently_open_class (scope));
23694}
23695
23696/* T is a SCOPE_REF; return whether we need to consider it
23697 instantiation-dependent so that we can check access at instantiation
23698 time even though we know which member it resolves to. */
23699
23700static bool
23701instantiation_dependent_scope_ref_p (tree t)
23702{
23703 if (DECL_P (TREE_OPERAND (t, 1))
23704 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23705 && accessible_in_template_p (TREE_OPERAND (t, 0),
23706 TREE_OPERAND (t, 1)))
23707 return false;
23708 else
23709 return true;
23710}
23711
23712/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23713 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23714 expression. */
23715
23716/* Note that this predicate is not appropriate for general expressions;
23717 only constant expressions (that satisfy potential_constant_expression)
23718 can be tested for value dependence. */
23719
23720bool
23721value_dependent_expression_p (tree expression)
23722{
23723 if (!processing_template_decl || expression == NULL_TREE)
23724 return false;
23725
23726 /* A name declared with a dependent type. */
23727 if (DECL_P (expression) && type_dependent_expression_p (expression))
23728 return true;
23729
23730 switch (TREE_CODE (expression))
23731 {
23732 case BASELINK:
23733 /* A dependent member function of the current instantiation. */
23734 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23735
23736 case FUNCTION_DECL:
23737 /* A dependent member function of the current instantiation. */
23738 if (DECL_CLASS_SCOPE_P (expression)
23739 && dependent_type_p (DECL_CONTEXT (expression)))
23740 return true;
23741 break;
23742
23743 case IDENTIFIER_NODE:
23744 /* A name that has not been looked up -- must be dependent. */
23745 return true;
23746
23747 case TEMPLATE_PARM_INDEX:
23748 /* A non-type template parm. */
23749 return true;
23750
23751 case CONST_DECL:
23752 /* A non-type template parm. */
23753 if (DECL_TEMPLATE_PARM_P (expression))
23754 return true;
23755 return value_dependent_expression_p (DECL_INITIAL (expression));
23756
23757 case VAR_DECL:
23758 /* A constant with literal type and is initialized
23759 with an expression that is value-dependent.
23760
23761 Note that a non-dependent parenthesized initializer will have
23762 already been replaced with its constant value, so if we see
23763 a TREE_LIST it must be dependent. */
23764 if (DECL_INITIAL (expression)
23765 && decl_constant_var_p (expression)
23766 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23767 /* cp_finish_decl doesn't fold reference initializers. */
23768 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23769 || type_dependent_expression_p (DECL_INITIAL (expression))
23770 || value_dependent_expression_p (DECL_INITIAL (expression))))
23771 return true;
23772 if (DECL_HAS_VALUE_EXPR_P (expression))
23773 {
23774 tree value_expr = DECL_VALUE_EXPR (expression);
23775 if (type_dependent_expression_p (value_expr))
23776 return true;
23777 }
23778 return false;
23779
23780 case DYNAMIC_CAST_EXPR:
23781 case STATIC_CAST_EXPR:
23782 case CONST_CAST_EXPR:
23783 case REINTERPRET_CAST_EXPR:
23784 case CAST_EXPR:
23785 /* These expressions are value-dependent if the type to which
23786 the cast occurs is dependent or the expression being casted
23787 is value-dependent. */
23788 {
23789 tree type = TREE_TYPE (expression);
23790
23791 if (dependent_type_p (type))
23792 return true;
23793
23794 /* A functional cast has a list of operands. */
23795 expression = TREE_OPERAND (expression, 0);
23796 if (!expression)
23797 {
23798 /* If there are no operands, it must be an expression such
23799 as "int()". This should not happen for aggregate types
23800 because it would form non-constant expressions. */
23801 gcc_assert (cxx_dialect >= cxx11
23802 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23803
23804 return false;
23805 }
23806
23807 if (TREE_CODE (expression) == TREE_LIST)
23808 return any_value_dependent_elements_p (expression);
23809
23810 return value_dependent_expression_p (expression);
23811 }
23812
23813 case SIZEOF_EXPR:
23814 if (SIZEOF_EXPR_TYPE_P (expression))
23815 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23816 /* FALLTHRU */
23817 case ALIGNOF_EXPR:
23818 case TYPEID_EXPR:
23819 /* A `sizeof' expression is value-dependent if the operand is
23820 type-dependent or is a pack expansion. */
23821 expression = TREE_OPERAND (expression, 0);
23822 if (PACK_EXPANSION_P (expression))
23823 return true;
23824 else if (TYPE_P (expression))
23825 return dependent_type_p (expression);
23826 return instantiation_dependent_uneval_expression_p (expression);
23827
23828 case AT_ENCODE_EXPR:
23829 /* An 'encode' expression is value-dependent if the operand is
23830 type-dependent. */
23831 expression = TREE_OPERAND (expression, 0);
23832 return dependent_type_p (expression);
23833
23834 case NOEXCEPT_EXPR:
23835 expression = TREE_OPERAND (expression, 0);
23836 return instantiation_dependent_uneval_expression_p (expression);
23837
23838 case SCOPE_REF:
23839 /* All instantiation-dependent expressions should also be considered
23840 value-dependent. */
23841 return instantiation_dependent_scope_ref_p (expression);
23842
23843 case COMPONENT_REF:
23844 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23845 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23846
23847 case NONTYPE_ARGUMENT_PACK:
23848 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23849 is value-dependent. */
23850 {
23851 tree values = ARGUMENT_PACK_ARGS (expression);
23852 int i, len = TREE_VEC_LENGTH (values);
23853
23854 for (i = 0; i < len; ++i)
23855 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23856 return true;
23857
23858 return false;
23859 }
23860
23861 case TRAIT_EXPR:
23862 {
23863 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23864 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23865 || (type2 ? dependent_type_p (type2) : false));
23866 }
23867
23868 case MODOP_EXPR:
23869 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23870 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23871
23872 case ARRAY_REF:
23873 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23874 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23875
23876 case ADDR_EXPR:
23877 {
23878 tree op = TREE_OPERAND (expression, 0);
23879 return (value_dependent_expression_p (op)
23880 || has_value_dependent_address (op));
23881 }
23882
23883 case REQUIRES_EXPR:
23884 /* Treat all requires-expressions as value-dependent so
23885 we don't try to fold them. */
23886 return true;
23887
23888 case TYPE_REQ:
23889 return dependent_type_p (TREE_OPERAND (expression, 0));
23890
23891 case CALL_EXPR:
23892 {
23893 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23894 return true;
23895 tree fn = get_callee_fndecl (expression);
23896 int i, nargs;
23897 nargs = call_expr_nargs (expression);
23898 for (i = 0; i < nargs; ++i)
23899 {
23900 tree op = CALL_EXPR_ARG (expression, i);
23901 /* In a call to a constexpr member function, look through the
23902 implicit ADDR_EXPR on the object argument so that it doesn't
23903 cause the call to be considered value-dependent. We also
23904 look through it in potential_constant_expression. */
23905 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23906 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23907 && TREE_CODE (op) == ADDR_EXPR)
23908 op = TREE_OPERAND (op, 0);
23909 if (value_dependent_expression_p (op))
23910 return true;
23911 }
23912 return false;
23913 }
23914
23915 case TEMPLATE_ID_EXPR:
23916 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23917 type-dependent. */
23918 return type_dependent_expression_p (expression)
23919 || variable_concept_p (TREE_OPERAND (expression, 0));
23920
23921 case CONSTRUCTOR:
23922 {
23923 unsigned ix;
23924 tree val;
23925 if (dependent_type_p (TREE_TYPE (expression)))
23926 return true;
23927 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23928 if (value_dependent_expression_p (val))
23929 return true;
23930 return false;
23931 }
23932
23933 case STMT_EXPR:
23934 /* Treat a GNU statement expression as dependent to avoid crashing
23935 under instantiate_non_dependent_expr; it can't be constant. */
23936 return true;
23937
23938 default:
23939 /* A constant expression is value-dependent if any subexpression is
23940 value-dependent. */
23941 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23942 {
23943 case tcc_reference:
23944 case tcc_unary:
23945 case tcc_comparison:
23946 case tcc_binary:
23947 case tcc_expression:
23948 case tcc_vl_exp:
23949 {
23950 int i, len = cp_tree_operand_length (expression);
23951
23952 for (i = 0; i < len; i++)
23953 {
23954 tree t = TREE_OPERAND (expression, i);
23955
23956 /* In some cases, some of the operands may be missing.l
23957 (For example, in the case of PREDECREMENT_EXPR, the
23958 amount to increment by may be missing.) That doesn't
23959 make the expression dependent. */
23960 if (t && value_dependent_expression_p (t))
23961 return true;
23962 }
23963 }
23964 break;
23965 default:
23966 break;
23967 }
23968 break;
23969 }
23970
23971 /* The expression is not value-dependent. */
23972 return false;
23973}
23974
23975/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23976 [temp.dep.expr]. Note that an expression with no type is
23977 considered dependent. Other parts of the compiler arrange for an
23978 expression with type-dependent subexpressions to have no type, so
23979 this function doesn't have to be fully recursive. */
23980
23981bool
23982type_dependent_expression_p (tree expression)
23983{
23984 if (!processing_template_decl)
23985 return false;
23986
23987 if (expression == NULL_TREE || expression == error_mark_node)
23988 return false;
23989
23990 /* An unresolved name is always dependent. */
23991 if (identifier_p (expression)
23992 || TREE_CODE (expression) == USING_DECL
23993 || TREE_CODE (expression) == WILDCARD_DECL)
23994 return true;
23995
23996 /* A fold expression is type-dependent. */
23997 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23998 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23999 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24000 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24001 return true;
24002
24003 /* Some expression forms are never type-dependent. */
24004 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24005 || TREE_CODE (expression) == SIZEOF_EXPR
24006 || TREE_CODE (expression) == ALIGNOF_EXPR
24007 || TREE_CODE (expression) == AT_ENCODE_EXPR
24008 || TREE_CODE (expression) == NOEXCEPT_EXPR
24009 || TREE_CODE (expression) == TRAIT_EXPR
24010 || TREE_CODE (expression) == TYPEID_EXPR
24011 || TREE_CODE (expression) == DELETE_EXPR
24012 || TREE_CODE (expression) == VEC_DELETE_EXPR
24013 || TREE_CODE (expression) == THROW_EXPR
24014 || TREE_CODE (expression) == REQUIRES_EXPR)
24015 return false;
24016
24017 /* The types of these expressions depends only on the type to which
24018 the cast occurs. */
24019 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24020 || TREE_CODE (expression) == STATIC_CAST_EXPR
24021 || TREE_CODE (expression) == CONST_CAST_EXPR
24022 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24023 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24024 || TREE_CODE (expression) == CAST_EXPR)
24025 return dependent_type_p (TREE_TYPE (expression));
24026
24027 /* The types of these expressions depends only on the type created
24028 by the expression. */
24029 if (TREE_CODE (expression) == NEW_EXPR
24030 || TREE_CODE (expression) == VEC_NEW_EXPR)
24031 {
24032 /* For NEW_EXPR tree nodes created inside a template, either
24033 the object type itself or a TREE_LIST may appear as the
24034 operand 1. */
24035 tree type = TREE_OPERAND (expression, 1);
24036 if (TREE_CODE (type) == TREE_LIST)
24037 /* This is an array type. We need to check array dimensions
24038 as well. */
24039 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24040 || value_dependent_expression_p
24041 (TREE_OPERAND (TREE_VALUE (type), 1));
24042 else
24043 return dependent_type_p (type);
24044 }
24045
24046 if (TREE_CODE (expression) == SCOPE_REF)
24047 {
24048 tree scope = TREE_OPERAND (expression, 0);
24049 tree name = TREE_OPERAND (expression, 1);
24050
24051 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24052 contains an identifier associated by name lookup with one or more
24053 declarations declared with a dependent type, or...a
24054 nested-name-specifier or qualified-id that names a member of an
24055 unknown specialization. */
24056 return (type_dependent_expression_p (name)
24057 || dependent_scope_p (scope));
24058 }
24059
24060 if (TREE_CODE (expression) == TEMPLATE_DECL
24061 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24062 return uses_outer_template_parms (expression);
24063
24064 if (TREE_CODE (expression) == STMT_EXPR)
24065 expression = stmt_expr_value_expr (expression);
24066
24067 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24068 {
24069 tree elt;
24070 unsigned i;
24071
24072 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24073 {
24074 if (type_dependent_expression_p (elt))
24075 return true;
24076 }
24077 return false;
24078 }
24079
24080 /* A static data member of the current instantiation with incomplete
24081 array type is type-dependent, as the definition and specializations
24082 can have different bounds. */
24083 if (VAR_P (expression)
24084 && DECL_CLASS_SCOPE_P (expression)
24085 && dependent_type_p (DECL_CONTEXT (expression))
24086 && VAR_HAD_UNKNOWN_BOUND (expression))
24087 return true;
24088
24089 /* An array of unknown bound depending on a variadic parameter, eg:
24090
24091 template<typename... Args>
24092 void foo (Args... args)
24093 {
24094 int arr[] = { args... };
24095 }
24096
24097 template<int... vals>
24098 void bar ()
24099 {
24100 int arr[] = { vals... };
24101 }
24102
24103 If the array has no length and has an initializer, it must be that
24104 we couldn't determine its length in cp_complete_array_type because
24105 it is dependent. */
24106 if (VAR_P (expression)
24107 && TREE_TYPE (expression) != NULL_TREE
24108 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24109 && !TYPE_DOMAIN (TREE_TYPE (expression))
24110 && DECL_INITIAL (expression))
24111 return true;
24112
24113 /* A function or variable template-id is type-dependent if it has any
24114 dependent template arguments. */
24115 if (VAR_OR_FUNCTION_DECL_P (expression)
24116 && DECL_LANG_SPECIFIC (expression)
24117 && DECL_TEMPLATE_INFO (expression))
24118 {
24119 /* Consider the innermost template arguments, since those are the ones
24120 that come from the template-id; the template arguments for the
24121 enclosing class do not make it type-dependent unless they are used in
24122 the type of the decl. */
24123 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24124 && (any_dependent_template_arguments_p
24125 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24126 return true;
24127 }
24128
24129 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24130 type-dependent. Checking this is important for functions with auto
24131 return type, which looks like a dependent type. */
24132 if (TREE_CODE (expression) == FUNCTION_DECL
24133 && undeduced_auto_decl (expression)
24134 && (!DECL_CLASS_SCOPE_P (expression)
24135 || !dependent_type_p (DECL_CONTEXT (expression)))
24136 && (!DECL_LANG_SPECIFIC (expression)
24137 || !DECL_FRIEND_CONTEXT (expression)
24138 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24139 && !DECL_LOCAL_FUNCTION_P (expression))
24140 {
24141 return false;
24142 }
24143
24144 /* Always dependent, on the number of arguments if nothing else. */
24145 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24146 return true;
24147
24148 if (TREE_TYPE (expression) == unknown_type_node)
24149 {
24150 if (TREE_CODE (expression) == ADDR_EXPR)
24151 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24152 if (TREE_CODE (expression) == COMPONENT_REF
24153 || TREE_CODE (expression) == OFFSET_REF)
24154 {
24155 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24156 return true;
24157 expression = TREE_OPERAND (expression, 1);
24158 if (identifier_p (expression))
24159 return false;
24160 }
24161 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24162 if (TREE_CODE (expression) == SCOPE_REF)
24163 return false;
24164
24165 if (BASELINK_P (expression))
24166 {
24167 if (BASELINK_OPTYPE (expression)
24168 && dependent_type_p (BASELINK_OPTYPE (expression)))
24169 return true;
24170 expression = BASELINK_FUNCTIONS (expression);
24171 }
24172
24173 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24174 {
24175 if (any_dependent_template_arguments_p
24176 (TREE_OPERAND (expression, 1)))
24177 return true;
24178 expression = TREE_OPERAND (expression, 0);
24179 if (identifier_p (expression))
24180 return true;
24181 }
24182
24183 gcc_assert (TREE_CODE (expression) == OVERLOAD
24184 || TREE_CODE (expression) == FUNCTION_DECL);
24185
24186 while (expression)
24187 {
24188 if (type_dependent_expression_p (OVL_CURRENT (expression)))
24189 return true;
24190 expression = OVL_NEXT (expression);
24191 }
24192 return false;
24193 }
24194
24195 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24196
24197 /* Dependent type attributes might not have made it from the decl to
24198 the type yet. */
24199 if (DECL_P (expression)
24200 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24201 return true;
24202
24203 return (dependent_type_p (TREE_TYPE (expression)));
24204}
24205
24206/* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24207 type-dependent if the expression refers to a member of the current
24208 instantiation and the type of the referenced member is dependent, or the
24209 class member access expression refers to a member of an unknown
24210 specialization.
24211
24212 This function returns true if the OBJECT in such a class member access
24213 expression is of an unknown specialization. */
24214
24215bool
24216type_dependent_object_expression_p (tree object)
24217{
24218 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24219 dependent. */
24220 if (TREE_CODE (object) == IDENTIFIER_NODE)
24221 return true;
24222 tree scope = TREE_TYPE (object);
24223 return (!scope || dependent_scope_p (scope));
24224}
24225
24226/* walk_tree callback function for instantiation_dependent_expression_p,
24227 below. Returns non-zero if a dependent subexpression is found. */
24228
24229static tree
24230instantiation_dependent_r (tree *tp, int *walk_subtrees,
24231 void * /*data*/)
24232{
24233 if (TYPE_P (*tp))
24234 {
24235 /* We don't have to worry about decltype currently because decltype
24236 of an instantiation-dependent expr is a dependent type. This
24237 might change depending on the resolution of DR 1172. */
24238 *walk_subtrees = false;
24239 return NULL_TREE;
24240 }
24241 enum tree_code code = TREE_CODE (*tp);
24242 switch (code)
24243 {
24244 /* Don't treat an argument list as dependent just because it has no
24245 TREE_TYPE. */
24246 case TREE_LIST:
24247 case TREE_VEC:
24248 return NULL_TREE;
24249
24250 case TEMPLATE_PARM_INDEX:
24251 return *tp;
24252
24253 /* Handle expressions with type operands. */
24254 case SIZEOF_EXPR:
24255 case ALIGNOF_EXPR:
24256 case TYPEID_EXPR:
24257 case AT_ENCODE_EXPR:
24258 {
24259 tree op = TREE_OPERAND (*tp, 0);
24260 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24261 op = TREE_TYPE (op);
24262 if (TYPE_P (op))
24263 {
24264 if (dependent_type_p (op))
24265 return *tp;
24266 else
24267 {
24268 *walk_subtrees = false;
24269 return NULL_TREE;
24270 }
24271 }
24272 break;
24273 }
24274
24275 case COMPONENT_REF:
24276 if (identifier_p (TREE_OPERAND (*tp, 1)))
24277 /* In a template, finish_class_member_access_expr creates a
24278 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24279 type-dependent, so that we can check access control at
24280 instantiation time (PR 42277). See also Core issue 1273. */
24281 return *tp;
24282 break;
24283
24284 case SCOPE_REF:
24285 if (instantiation_dependent_scope_ref_p (*tp))
24286 return *tp;
24287 else
24288 break;
24289
24290 /* Treat statement-expressions as dependent. */
24291 case BIND_EXPR:
24292 return *tp;
24293
24294 /* Treat requires-expressions as dependent. */
24295 case REQUIRES_EXPR:
24296 return *tp;
24297
24298 case CALL_EXPR:
24299 /* Treat calls to function concepts as dependent. */
24300 if (function_concept_check_p (*tp))
24301 return *tp;
24302 break;
24303
24304 case TEMPLATE_ID_EXPR:
24305 /* And variable concepts. */
24306 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24307 return *tp;
24308 break;
24309
24310 default:
24311 break;
24312 }
24313
24314 if (type_dependent_expression_p (*tp))
24315 return *tp;
24316 else
24317 return NULL_TREE;
24318}
24319
24320/* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24321 sense defined by the ABI:
24322
24323 "An expression is instantiation-dependent if it is type-dependent
24324 or value-dependent, or it has a subexpression that is type-dependent
24325 or value-dependent."
24326
24327 Except don't actually check value-dependence for unevaluated expressions,
24328 because in sizeof(i) we don't care about the value of i. Checking
24329 type-dependence will in turn check value-dependence of array bounds/template
24330 arguments as needed. */
24331
24332bool
24333instantiation_dependent_uneval_expression_p (tree expression)
24334{
24335 tree result;
24336
24337 if (!processing_template_decl)
24338 return false;
24339
24340 if (expression == error_mark_node)
24341 return false;
24342
24343 result = cp_walk_tree_without_duplicates (&expression,
24344 instantiation_dependent_r, NULL);
24345 return result != NULL_TREE;
24346}
24347
24348/* As above, but also check value-dependence of the expression as a whole. */
24349
24350bool
24351instantiation_dependent_expression_p (tree expression)
24352{
24353 return (instantiation_dependent_uneval_expression_p (expression)
24354 || value_dependent_expression_p (expression));
24355}
24356
24357/* Like type_dependent_expression_p, but it also works while not processing
24358 a template definition, i.e. during substitution or mangling. */
24359
24360bool
24361type_dependent_expression_p_push (tree expr)
24362{
24363 bool b;
24364 ++processing_template_decl;
24365 b = type_dependent_expression_p (expr);
24366 --processing_template_decl;
24367 return b;
24368}
24369
24370/* Returns TRUE if ARGS contains a type-dependent expression. */
24371
24372bool
24373any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24374{
24375 unsigned int i;
24376 tree arg;
24377
24378 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24379 {
24380 if (type_dependent_expression_p (arg))
24381 return true;
24382 }
24383 return false;
24384}
24385
24386/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24387 expressions) contains any type-dependent expressions. */
24388
24389bool
24390any_type_dependent_elements_p (const_tree list)
24391{
24392 for (; list; list = TREE_CHAIN (list))
24393 if (type_dependent_expression_p (TREE_VALUE (list)))
24394 return true;
24395
24396 return false;
24397}
24398
24399/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24400 expressions) contains any value-dependent expressions. */
24401
24402bool
24403any_value_dependent_elements_p (const_tree list)
24404{
24405 for (; list; list = TREE_CHAIN (list))
24406 if (value_dependent_expression_p (TREE_VALUE (list)))
24407 return true;
24408
24409 return false;
24410}
24411
24412/* Returns TRUE if the ARG (a template argument) is dependent. */
24413
24414bool
24415dependent_template_arg_p (tree arg)
24416{
24417 if (!processing_template_decl)
24418 return false;
24419
24420 /* Assume a template argument that was wrongly written by the user
24421 is dependent. This is consistent with what
24422 any_dependent_template_arguments_p [that calls this function]
24423 does. */
24424 if (!arg || arg == error_mark_node)
24425 return true;
24426
24427 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24428 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24429
24430 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24431 return true;
24432 if (TREE_CODE (arg) == TEMPLATE_DECL)
24433 {
24434 if (DECL_TEMPLATE_PARM_P (arg))
24435 return true;
24436 /* A member template of a dependent class is not necessarily
24437 type-dependent, but it is a dependent template argument because it
24438 will be a member of an unknown specialization to that template. */
24439 tree scope = CP_DECL_CONTEXT (arg);
24440 return TYPE_P (scope) && dependent_type_p (scope);
24441 }
24442 else if (ARGUMENT_PACK_P (arg))
24443 {
24444 tree args = ARGUMENT_PACK_ARGS (arg);
24445 int i, len = TREE_VEC_LENGTH (args);
24446 for (i = 0; i < len; ++i)
24447 {
24448 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24449 return true;
24450 }
24451
24452 return false;
24453 }
24454 else if (TYPE_P (arg))
24455 return dependent_type_p (arg);
24456 else
24457 return (type_dependent_expression_p (arg)
24458 || value_dependent_expression_p (arg));
24459}
24460
24461/* Returns true if ARGS (a collection of template arguments) contains
24462 any types that require structural equality testing. */
24463
24464bool
24465any_template_arguments_need_structural_equality_p (tree args)
24466{
24467 int i;
24468 int j;
24469
24470 if (!args)
24471 return false;
24472 if (args == error_mark_node)
24473 return true;
24474
24475 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24476 {
24477 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24478 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24479 {
24480 tree arg = TREE_VEC_ELT (level, j);
24481 tree packed_args = NULL_TREE;
24482 int k, len = 1;
24483
24484 if (ARGUMENT_PACK_P (arg))
24485 {
24486 /* Look inside the argument pack. */
24487 packed_args = ARGUMENT_PACK_ARGS (arg);
24488 len = TREE_VEC_LENGTH (packed_args);
24489 }
24490
24491 for (k = 0; k < len; ++k)
24492 {
24493 if (packed_args)
24494 arg = TREE_VEC_ELT (packed_args, k);
24495
24496 if (error_operand_p (arg))
24497 return true;
24498 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24499 continue;
24500 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24501 return true;
24502 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24503 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24504 return true;
24505 }
24506 }
24507 }
24508
24509 return false;
24510}
24511
24512/* Returns true if ARGS (a collection of template arguments) contains
24513 any dependent arguments. */
24514
24515bool
24516any_dependent_template_arguments_p (const_tree args)
24517{
24518 int i;
24519 int j;
24520
24521 if (!args)
24522 return false;
24523 if (args == error_mark_node)
24524 return true;
24525
24526 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24527 {
24528 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24529 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24530 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24531 return true;
24532 }
24533
24534 return false;
24535}
24536
24537/* Returns TRUE if the template TMPL is type-dependent. */
24538
24539bool
24540dependent_template_p (tree tmpl)
24541{
24542 if (TREE_CODE (tmpl) == OVERLOAD)
24543 {
24544 while (tmpl)
24545 {
24546 if (dependent_template_p (OVL_CURRENT (tmpl)))
24547 return true;
24548 tmpl = OVL_NEXT (tmpl);
24549 }
24550 return false;
24551 }
24552
24553 /* Template template parameters are dependent. */
24554 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24555 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24556 return true;
24557 /* So are names that have not been looked up. */
24558 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24559 return true;
24560 return false;
24561}
24562
24563/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24564
24565bool
24566dependent_template_id_p (tree tmpl, tree args)
24567{
24568 return (dependent_template_p (tmpl)
24569 || any_dependent_template_arguments_p (args));
24570}
24571
24572/* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24573 are dependent. */
24574
24575bool
24576dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24577{
24578 int i;
24579
24580 if (!processing_template_decl)
24581 return false;
24582
24583 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24584 {
24585 tree decl = TREE_VEC_ELT (declv, i);
24586 tree init = TREE_VEC_ELT (initv, i);
24587 tree cond = TREE_VEC_ELT (condv, i);
24588 tree incr = TREE_VEC_ELT (incrv, i);
24589
24590 if (type_dependent_expression_p (decl)
24591 || TREE_CODE (decl) == SCOPE_REF)
24592 return true;
24593
24594 if (init && type_dependent_expression_p (init))
24595 return true;
24596
24597 if (type_dependent_expression_p (cond))
24598 return true;
24599
24600 if (COMPARISON_CLASS_P (cond)
24601 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24602 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24603 return true;
24604
24605 if (TREE_CODE (incr) == MODOP_EXPR)
24606 {
24607 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24608 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24609 return true;
24610 }
24611 else if (type_dependent_expression_p (incr))
24612 return true;
24613 else if (TREE_CODE (incr) == MODIFY_EXPR)
24614 {
24615 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24616 return true;
24617 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24618 {
24619 tree t = TREE_OPERAND (incr, 1);
24620 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24621 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24622 return true;
24623 }
24624 }
24625 }
24626
24627 return false;
24628}
24629
24630/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24631 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24632 no such TYPE can be found. Note that this function peers inside
24633 uninstantiated templates and therefore should be used only in
24634 extremely limited situations. ONLY_CURRENT_P restricts this
24635 peering to the currently open classes hierarchy (which is required
24636 when comparing types). */
24637
24638tree
24639resolve_typename_type (tree type, bool only_current_p)
24640{
24641 tree scope;
24642 tree name;
24643 tree decl;
24644 int quals;
24645 tree pushed_scope;
24646 tree result;
24647
24648 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24649
24650 scope = TYPE_CONTEXT (type);
24651 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24652 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24653 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24654 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24655 identifier of the TYPENAME_TYPE anymore.
24656 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24657 TYPENAME_TYPE instead, we avoid messing up with a possible
24658 typedef variant case. */
24659 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24660
24661 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24662 it first before we can figure out what NAME refers to. */
24663 if (TREE_CODE (scope) == TYPENAME_TYPE)
24664 {
24665 if (TYPENAME_IS_RESOLVING_P (scope))
24666 /* Given a class template A with a dependent base with nested type C,
24667 typedef typename A::C::C C will land us here, as trying to resolve
24668 the initial A::C leads to the local C typedef, which leads back to
24669 A::C::C. So we break the recursion now. */
24670 return type;
24671 else
24672 scope = resolve_typename_type (scope, only_current_p);
24673 }
24674 /* If we don't know what SCOPE refers to, then we cannot resolve the
24675 TYPENAME_TYPE. */
24676 if (!CLASS_TYPE_P (scope))
24677 return type;
24678 /* If this is a typedef, we don't want to look inside (c++/11987). */
24679 if (typedef_variant_p (type))
24680 return type;
24681 /* If SCOPE isn't the template itself, it will not have a valid
24682 TYPE_FIELDS list. */
24683 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24684 /* scope is either the template itself or a compatible instantiation
24685 like X<T>, so look up the name in the original template. */
24686 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24687 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24688 gcc_checking_assert (uses_template_parms (scope));
24689 /* If scope has no fields, it can't be a current instantiation. Check this
24690 before currently_open_class to avoid infinite recursion (71515). */
24691 if (!TYPE_FIELDS (scope))
24692 return type;
24693 /* If the SCOPE is not the current instantiation, there's no reason
24694 to look inside it. */
24695 if (only_current_p && !currently_open_class (scope))
24696 return type;
24697 /* Enter the SCOPE so that name lookup will be resolved as if we
24698 were in the class definition. In particular, SCOPE will no
24699 longer be considered a dependent type. */
24700 pushed_scope = push_scope (scope);
24701 /* Look up the declaration. */
24702 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24703 tf_warning_or_error);
24704
24705 result = NULL_TREE;
24706
24707 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24708 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24709 if (!decl)
24710 /*nop*/;
24711 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24712 && TREE_CODE (decl) == TYPE_DECL)
24713 {
24714 result = TREE_TYPE (decl);
24715 if (result == error_mark_node)
24716 result = NULL_TREE;
24717 }
24718 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24719 && DECL_CLASS_TEMPLATE_P (decl))
24720 {
24721 tree tmpl;
24722 tree args;
24723 /* Obtain the template and the arguments. */
24724 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24725 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24726 /* Instantiate the template. */
24727 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24728 /*entering_scope=*/0,
24729 tf_error | tf_user);
24730 if (result == error_mark_node)
24731 result = NULL_TREE;
24732 }
24733
24734 /* Leave the SCOPE. */
24735 if (pushed_scope)
24736 pop_scope (pushed_scope);
24737
24738 /* If we failed to resolve it, return the original typename. */
24739 if (!result)
24740 return type;
24741
24742 /* If lookup found a typename type, resolve that too. */
24743 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24744 {
24745 /* Ill-formed programs can cause infinite recursion here, so we
24746 must catch that. */
24747 TYPENAME_IS_RESOLVING_P (result) = 1;
24748 result = resolve_typename_type (result, only_current_p);
24749 TYPENAME_IS_RESOLVING_P (result) = 0;
24750 }
24751
24752 /* Qualify the resulting type. */
24753 quals = cp_type_quals (type);
24754 if (quals)
24755 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24756
24757 return result;
24758}
24759
24760/* EXPR is an expression which is not type-dependent. Return a proxy
24761 for EXPR that can be used to compute the types of larger
24762 expressions containing EXPR. */
24763
24764tree
24765build_non_dependent_expr (tree expr)
24766{
24767 tree inner_expr;
24768
24769 /* When checking, try to get a constant value for all non-dependent
24770 expressions in order to expose bugs in *_dependent_expression_p
24771 and constexpr. This can affect code generation, see PR70704, so
24772 only do this for -fchecking=2. */
24773 if (flag_checking > 1
24774 && cxx_dialect >= cxx11
24775 /* Don't do this during nsdmi parsing as it can lead to
24776 unexpected recursive instantiations. */
24777 && !parsing_nsdmi ()
24778 /* Don't do this during concept expansion either and for
24779 the same reason. */
24780 && !expanding_concept ())
24781 fold_non_dependent_expr (expr);
24782
24783 /* Preserve OVERLOADs; the functions must be available to resolve
24784 types. */
24785 inner_expr = expr;
24786 if (TREE_CODE (inner_expr) == STMT_EXPR)
24787 inner_expr = stmt_expr_value_expr (inner_expr);
24788 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24789 inner_expr = TREE_OPERAND (inner_expr, 0);
24790 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24791 inner_expr = TREE_OPERAND (inner_expr, 1);
24792 if (is_overloaded_fn (inner_expr)
24793 || TREE_CODE (inner_expr) == OFFSET_REF)
24794 return expr;
24795 /* There is no need to return a proxy for a variable. */
24796 if (VAR_P (expr))
24797 return expr;
24798 /* Preserve string constants; conversions from string constants to
24799 "char *" are allowed, even though normally a "const char *"
24800 cannot be used to initialize a "char *". */
24801 if (TREE_CODE (expr) == STRING_CST)
24802 return expr;
24803 /* Preserve void and arithmetic constants, as an optimization -- there is no
24804 reason to create a new node. */
24805 if (TREE_CODE (expr) == VOID_CST
24806 || TREE_CODE (expr) == INTEGER_CST
24807 || TREE_CODE (expr) == REAL_CST)
24808 return expr;
24809 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24810 There is at least one place where we want to know that a
24811 particular expression is a throw-expression: when checking a ?:
24812 expression, there are special rules if the second or third
24813 argument is a throw-expression. */
24814 if (TREE_CODE (expr) == THROW_EXPR)
24815 return expr;
24816
24817 /* Don't wrap an initializer list, we need to be able to look inside. */
24818 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24819 return expr;
24820
24821 /* Don't wrap a dummy object, we need to be able to test for it. */
24822 if (is_dummy_object (expr))
24823 return expr;
24824
24825 if (TREE_CODE (expr) == COND_EXPR)
24826 return build3 (COND_EXPR,
24827 TREE_TYPE (expr),
24828 TREE_OPERAND (expr, 0),
24829 (TREE_OPERAND (expr, 1)
24830 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24831 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24832 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24833 if (TREE_CODE (expr) == COMPOUND_EXPR
24834 && !COMPOUND_EXPR_OVERLOADED (expr))
24835 return build2 (COMPOUND_EXPR,
24836 TREE_TYPE (expr),
24837 TREE_OPERAND (expr, 0),
24838 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24839
24840 /* If the type is unknown, it can't really be non-dependent */
24841 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24842
24843 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24844 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24845}
24846
24847/* ARGS is a vector of expressions as arguments to a function call.
24848 Replace the arguments with equivalent non-dependent expressions.
24849 This modifies ARGS in place. */
24850
24851void
24852make_args_non_dependent (vec<tree, va_gc> *args)
24853{
24854 unsigned int ix;
24855 tree arg;
24856
24857 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24858 {
24859 tree newarg = build_non_dependent_expr (arg);
24860 if (newarg != arg)
24861 (*args)[ix] = newarg;
24862 }
24863}
24864
24865/* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24866 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24867 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24868
24869static tree
24870make_auto_1 (tree name, bool set_canonical)
24871{
24872 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24873 TYPE_NAME (au) = build_decl (input_location,
24874 TYPE_DECL, name, au);
24875 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24876 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24877 (0, processing_template_decl + 1, processing_template_decl + 1,
24878 TYPE_NAME (au), NULL_TREE);
24879 if (set_canonical)
24880 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24881 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24882 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24883
24884 return au;
24885}
24886
24887tree
24888make_decltype_auto (void)
24889{
24890 return make_auto_1 (decltype_auto_identifier, true);
24891}
24892
24893tree
24894make_auto (void)
24895{
24896 return make_auto_1 (auto_identifier, true);
24897}
24898
24899/* Return a C++17 deduction placeholder for class template TMPL. */
24900
24901tree
24902make_template_placeholder (tree tmpl)
24903{
24904 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24905 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24906 return t;
24907}
24908
24909/* True iff T is a C++17 class template deduction placeholder. */
24910
24911bool
24912template_placeholder_p (tree t)
24913{
24914 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
24915}
24916
24917/* Make a "constrained auto" type-specifier. This is an
24918 auto type with constraints that must be associated after
24919 deduction. The constraint is formed from the given
24920 CONC and its optional sequence of arguments, which are
24921 non-null if written as partial-concept-id. */
24922
24923tree
24924make_constrained_auto (tree con, tree args)
24925{
24926 tree type = make_auto_1 (auto_identifier, false);
24927
24928 /* Build the constraint. */
24929 tree tmpl = DECL_TI_TEMPLATE (con);
24930 tree expr;
24931 if (VAR_P (con))
24932 expr = build_concept_check (tmpl, type, args);
24933 else
24934 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24935
24936 tree constr = normalize_expression (expr);
24937 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24938
24939 /* Our canonical type depends on the constraint. */
24940 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24941
24942 /* Attach the constraint to the type declaration. */
24943 tree decl = TYPE_NAME (type);
24944 return decl;
24945}
24946
24947/* Given type ARG, return std::initializer_list<ARG>. */
24948
24949static tree
24950listify (tree arg)
24951{
24952 tree std_init_list = namespace_binding
24953 (get_identifier ("initializer_list"), std_node);
24954 tree argvec;
24955 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24956 {
24957 error ("deducing from brace-enclosed initializer list requires "
24958 "#include <initializer_list>");
24959 return error_mark_node;
24960 }
24961 argvec = make_tree_vec (1);
24962 TREE_VEC_ELT (argvec, 0) = arg;
24963 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24964 NULL_TREE, 0, tf_warning_or_error);
24965}
24966
24967/* Replace auto in TYPE with std::initializer_list<auto>. */
24968
24969static tree
24970listify_autos (tree type, tree auto_node)
24971{
24972 tree init_auto = listify (auto_node);
24973 tree argvec = make_tree_vec (1);
24974 TREE_VEC_ELT (argvec, 0) = init_auto;
24975 if (processing_template_decl)
24976 argvec = add_to_template_args (current_template_args (), argvec);
24977 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24978}
24979
24980/* Hash traits for hashing possibly constrained 'auto'
24981 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24982
24983struct auto_hash : default_hash_traits<tree>
24984{
24985 static inline hashval_t hash (tree);
24986 static inline bool equal (tree, tree);
24987};
24988
24989/* Hash the 'auto' T. */
24990
24991inline hashval_t
24992auto_hash::hash (tree t)
24993{
24994 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24995 /* Matching constrained-type-specifiers denote the same template
24996 parameter, so hash the constraint. */
24997 return hash_placeholder_constraint (c);
24998 else
24999 /* But unconstrained autos are all separate, so just hash the pointer. */
25000 return iterative_hash_object (t, 0);
25001}
25002
25003/* Compare two 'auto's. */
25004
25005inline bool
25006auto_hash::equal (tree t1, tree t2)
25007{
25008 if (t1 == t2)
25009 return true;
25010
25011 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25012 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25013
25014 /* Two unconstrained autos are distinct. */
25015 if (!c1 || !c2)
25016 return false;
25017
25018 return equivalent_placeholder_constraints (c1, c2);
25019}
25020
25021/* for_each_template_parm callback for extract_autos: if t is a (possibly
25022 constrained) auto, add it to the vector. */
25023
25024static int
25025extract_autos_r (tree t, void *data)
25026{
25027 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25028 if (is_auto_or_concept (t))
25029 {
25030 /* All the autos were built with index 0; fix that up now. */
25031 tree *p = hash.find_slot (t, INSERT);
25032 unsigned idx;
25033 if (*p)
25034 /* If this is a repeated constrained-type-specifier, use the index we
25035 chose before. */
25036 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25037 else
25038 {
25039 /* Otherwise this is new, so use the current count. */
25040 *p = t;
25041 idx = hash.elements () - 1;
25042 }
25043 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25044 }
25045
25046 /* Always keep walking. */
25047 return 0;
25048}
25049
25050/* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25051 says they can appear anywhere in the type. */
25052
25053static tree
25054extract_autos (tree type)
25055{
25056 hash_set<tree> visited;
25057 hash_table<auto_hash> hash (2);
25058
25059 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25060
25061 tree tree_vec = make_tree_vec (hash.elements());
25062 for (hash_table<auto_hash>::iterator iter = hash.begin();
25063 iter != hash.end(); ++iter)
25064 {
25065 tree elt = *iter;
25066 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25067 TREE_VEC_ELT (tree_vec, i)
25068 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25069 }
25070
25071 return tree_vec;
25072}
25073
25074/* The stem for deduction guide names. */
25075const char *const dguide_base = "__dguide_";
25076
25077/* Return the name for a deduction guide for class template TMPL. */
25078
25079tree
25080dguide_name (tree tmpl)
25081{
25082 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25083 tree tname = TYPE_IDENTIFIER (type);
25084 char *buf = (char *) alloca (1 + strlen (dguide_base)
25085 + IDENTIFIER_LENGTH (tname));
25086 memcpy (buf, dguide_base, strlen (dguide_base));
25087 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25088 IDENTIFIER_LENGTH (tname) + 1);
25089 tree dname = get_identifier (buf);
25090 TREE_TYPE (dname) = type;
25091 return dname;
25092}
25093
25094/* True if NAME is the name of a deduction guide. */
25095
25096bool
25097dguide_name_p (tree name)
25098{
25099 return (TREE_TYPE (name)
25100 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25101 strlen (dguide_base)));
25102}
25103
25104/* True if FN is a deduction guide. */
25105
25106bool
25107deduction_guide_p (const_tree fn)
25108{
25109 if (DECL_P (fn))
25110 if (tree name = DECL_NAME (fn))
25111 return dguide_name_p (name);
25112 return false;
25113}
25114
25115/* True if FN is the copy deduction guide, i.e. A(A)->A. */
25116
25117bool
25118copy_guide_p (const_tree fn)
25119{
25120 gcc_assert (deduction_guide_p (fn));
25121 if (!DECL_ARTIFICIAL (fn))
25122 return false;
25123 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25124 return (TREE_CHAIN (parms) == void_list_node
25125 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25126}
25127
25128/* True if FN is a guide generated from a constructor template. */
25129
25130bool
25131template_guide_p (const_tree fn)
25132{
25133 gcc_assert (deduction_guide_p (fn));
25134 if (!DECL_ARTIFICIAL (fn))
25135 return false;
25136 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25137 {
25138 tree tmpl = DECL_TI_TEMPLATE (ctor);
25139 return PRIMARY_TEMPLATE_P (tmpl);
25140 }
25141 return false;
25142}
25143
25144/* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25145 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25146 template parameter types. Note that the handling of template template
25147 parameters relies on current_template_parms being set appropriately for the
25148 new template. */
25149
25150static tree
25151rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25152 tree tsubst_args, tsubst_flags_t complain)
25153{
25154 tree oldidx = get_template_parm_index (olddecl);
25155
25156 tree newtype;
25157 if (TREE_CODE (olddecl) == TYPE_DECL
25158 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25159 {
25160 tree oldtype = TREE_TYPE (olddecl);
25161 newtype = cxx_make_type (TREE_CODE (oldtype));
25162 TYPE_MAIN_VARIANT (newtype) = newtype;
25163 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25164 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25165 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25166 }
25167 else
25168 {
25169 newtype = TREE_TYPE (olddecl);
25170 if (type_uses_auto (newtype))
25171 {
25172 // Substitute once to fix references to other template parameters.
25173 newtype = tsubst (newtype, tsubst_args,
25174 complain|tf_partial, NULL_TREE);
25175 // Now substitute again to reduce the level of the auto.
25176 newtype = tsubst (newtype, current_template_args (),
25177 complain, NULL_TREE);
25178 }
25179 else
25180 newtype = tsubst (newtype, tsubst_args,
25181 complain, NULL_TREE);
25182 }
25183
25184 tree newdecl
25185 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25186 DECL_NAME (olddecl), newtype);
25187 SET_DECL_TEMPLATE_PARM_P (newdecl);
25188
25189 tree newidx;
25190 if (TREE_CODE (olddecl) == TYPE_DECL
25191 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25192 {
25193 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25194 = build_template_parm_index (index, level, level,
25195 newdecl, newtype);
25196 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25197 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25198 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25199 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25200
25201 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25202 {
25203 DECL_TEMPLATE_RESULT (newdecl)
25204 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25205 DECL_NAME (olddecl), newtype);
25206 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25207 // First create a copy (ttargs) of tsubst_args with an
25208 // additional level for the template template parameter's own
25209 // template parameters (ttparms).
25210 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25211 (DECL_TEMPLATE_PARMS (olddecl)));
25212 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25213 tree ttargs = make_tree_vec (depth + 1);
25214 for (int i = 0; i < depth; ++i)
25215 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25216 TREE_VEC_ELT (ttargs, depth)
25217 = template_parms_level_to_args (ttparms);
25218 // Substitute ttargs into ttparms to fix references to
25219 // other template parameters.
25220 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25221 complain|tf_partial);
25222 // Now substitute again with args based on tparms, to reduce
25223 // the level of the ttparms.
25224 ttargs = current_template_args ();
25225 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25226 complain);
25227 // Finally, tack the adjusted parms onto tparms.
25228 ttparms = tree_cons (size_int (depth), ttparms,
25229 current_template_parms);
25230 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25231 }
25232 }
25233 else
25234 {
25235 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25236 tree newconst
25237 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25238 TREE_CODE (oldconst),
25239 DECL_NAME (oldconst), newtype);
25240 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25241 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25242 SET_DECL_TEMPLATE_PARM_P (newconst);
25243 newidx = build_template_parm_index (index, level, level,
25244 newconst, newtype);
25245 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25246 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25247 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25248 }
25249
25250 return newdecl;
25251}
25252
25253/* Returns a C++17 class deduction guide template based on the constructor
25254 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25255 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25256
25257static tree
25258build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25259{
25260 tree type, tparms, targs, fparms, fargs, ci;
25261 bool memtmpl = false;
25262 bool explicit_p;
25263 location_t loc;
25264
25265 if (TYPE_P (ctor))
25266 {
25267 type = ctor;
25268 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25269 if (copy_p)
25270 {
25271 type = TREE_TYPE (type);
25272 fparms = tree_cons (NULL_TREE, type, void_list_node);
25273 }
25274 else
25275 fparms = void_list_node;
25276
25277 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25278 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25279 targs = CLASSTYPE_TI_ARGS (type);
25280 ci = NULL_TREE;
25281 fargs = NULL_TREE;
25282 loc = DECL_SOURCE_LOCATION (ctmpl);
25283 explicit_p = false;
25284 }
25285 else
25286 {
25287 ++processing_template_decl;
25288
25289 tree fn_tmpl
25290 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25291 : DECL_TI_TEMPLATE (ctor));
25292 if (outer_args)
25293 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25294 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25295
25296 type = DECL_CONTEXT (ctor);
25297
25298 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25299 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25300 fully specialized args for the enclosing class. Strip those off, as
25301 the deduction guide won't have those template parameters. */
25302 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25303 TMPL_PARMS_DEPTH (tparms));
25304 /* Discard the 'this' parameter. */
25305 fparms = FUNCTION_ARG_CHAIN (ctor);
25306 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25307 ci = get_constraints (ctor);
25308 loc = DECL_SOURCE_LOCATION (ctor);
25309 explicit_p = DECL_NONCONVERTING_P (ctor);
25310
25311 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25312 {
25313 memtmpl = true;
25314
25315 /* For a member template constructor, we need to flatten the two
25316 template parameter lists into one, and then adjust the function
25317 signature accordingly. This gets...complicated. */
25318 tree save_parms = current_template_parms;
25319
25320 /* For a member template we should have two levels of parms/args, one
25321 for the class and one for the constructor. We stripped
25322 specialized args for further enclosing classes above. */
25323 const int depth = 2;
25324 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25325
25326 /* Template args for translating references to the two-level template
25327 parameters into references to the one-level template parameters we
25328 are creating. */
25329 tree tsubst_args = copy_node (targs);
25330 TMPL_ARGS_LEVEL (tsubst_args, depth)
25331 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25332
25333 /* Template parms for the constructor template. */
25334 tree ftparms = TREE_VALUE (tparms);
25335 unsigned flen = TREE_VEC_LENGTH (ftparms);
25336 /* Template parms for the class template. */
25337 tparms = TREE_CHAIN (tparms);
25338 tree ctparms = TREE_VALUE (tparms);
25339 unsigned clen = TREE_VEC_LENGTH (ctparms);
25340 /* Template parms for the deduction guide start as a copy of the
25341 template parms for the class. We set current_template_parms for
25342 lookup_template_class_1. */
25343 current_template_parms = tparms = copy_node (tparms);
25344 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25345 for (unsigned i = 0; i < clen; ++i)
25346 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25347
25348 /* Now we need to rewrite the constructor parms to append them to the
25349 class parms. */
25350 for (unsigned i = 0; i < flen; ++i)
25351 {
25352 unsigned index = i + clen;
25353 unsigned level = 1;
25354 tree oldelt = TREE_VEC_ELT (ftparms, i);
25355 tree olddecl = TREE_VALUE (oldelt);
25356 tree newdecl = rewrite_template_parm (olddecl, index, level,
25357 tsubst_args, complain);
25358 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25359 tsubst_args, complain, ctor);
25360 tree list = build_tree_list (newdef, newdecl);
25361 TEMPLATE_PARM_CONSTRAINTS (list)
25362 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25363 tsubst_args, complain, ctor);
25364 TREE_VEC_ELT (new_vec, index) = list;
25365 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25366 }
25367
25368 /* Now we have a final set of template parms to substitute into the
25369 function signature. */
25370 targs = template_parms_to_args (tparms);
25371 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25372 complain, ctor);
25373 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25374 if (ci)
25375 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25376
25377 current_template_parms = save_parms;
25378 }
25379 --processing_template_decl;
25380 }
25381
25382 if (!memtmpl)
25383 {
25384 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25385 tparms = copy_node (tparms);
25386 INNERMOST_TEMPLATE_PARMS (tparms)
25387 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25388 }
25389
25390 tree fntype = build_function_type (type, fparms);
25391 tree ded_fn = build_lang_decl_loc (loc,
25392 FUNCTION_DECL,
25393 dguide_name (type), fntype);
25394 DECL_ARGUMENTS (ded_fn) = fargs;
25395 DECL_ARTIFICIAL (ded_fn) = true;
25396 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25397 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25398 DECL_ARTIFICIAL (ded_tmpl) = true;
25399 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25400 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25401 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25402 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25403 if (DECL_P (ctor))
25404 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25405 if (ci)
25406 set_constraints (ded_tmpl, ci);
25407
25408 return ded_tmpl;
25409}
25410
25411/* Deduce template arguments for the class template placeholder PTYPE for
25412 template TMPL based on the initializer INIT, and return the resulting
25413 type. */
25414
25415static tree
25416do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25417 tsubst_flags_t complain)
25418{
25419 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25420 {
25421 /* We should have handled this in the caller. */
25422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25423 return ptype;
25424 if (complain & tf_error)
25425 error ("non-class template %qT used without template arguments", tmpl);
25426 return error_mark_node;
25427 }
25428
25429 tree type = TREE_TYPE (tmpl);
25430
25431 vec<tree,va_gc> *args;
25432 if (init == NULL_TREE
25433 || TREE_CODE (init) == TREE_LIST)
25434 args = make_tree_vector_from_list (init);
25435 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25436 && !TYPE_HAS_LIST_CTOR (type)
25437 && !is_std_init_list (type))
25438 args = make_tree_vector_from_ctor (init);
25439 else
25440 args = make_tree_vector_single (init);
25441
25442 tree dname = dguide_name (tmpl);
25443 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25444 /*type*/false, /*complain*/false,
25445 /*hidden*/false);
25446 if (cands == error_mark_node)
25447 cands = NULL_TREE;
25448
25449 tree outer_args = NULL_TREE;
25450 if (DECL_CLASS_SCOPE_P (tmpl)
25451 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25452 {
25453 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25454 type = TREE_TYPE (most_general_template (tmpl));
25455 }
25456
25457 bool saw_ctor = false;
25458 if (CLASSTYPE_METHOD_VEC (type))
25459 // FIXME cache artificial deduction guides
25460 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25461 {
25462 if (TREE_CODE (fns) == OVERLOAD && OVL_USED (fns))
25463 continue;
25464
25465 tree fn = OVL_CURRENT (fns);
25466 tree guide = build_deduction_guide (fn, outer_args, complain);
25467 cands = ovl_cons (guide, cands);
25468
25469 saw_ctor = true;
25470 }
25471
25472 if (!saw_ctor && args->length() == 0)
25473 {
25474 tree guide = build_deduction_guide (type, outer_args, complain);
25475 cands = ovl_cons (guide, cands);
25476 }
25477 if (args->length() == 1)
25478 {
25479 tree guide = build_deduction_guide (build_reference_type (type),
25480 outer_args, complain);
25481 cands = ovl_cons (guide, cands);
25482 }
25483
25484 /* Prune explicit deduction guides in copy-initialization context. */
25485 tree old_cands = cands;
25486 if (flags & LOOKUP_ONLYCONVERTING)
25487 {
25488 tree t = cands;
25489 for (; t; t = OVL_NEXT (t))
25490 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25491 break;
25492 if (t)
25493 {
25494 tree pruned = NULL_TREE;
25495 for (t = cands; t; t = OVL_NEXT (t))
25496 {
25497 tree f = OVL_CURRENT (t);
25498 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25499 pruned = build_overload (f, pruned);
25500 }
25501 cands = pruned;
25502 if (cands == NULL_TREE)
25503 {
25504 error ("cannot deduce template arguments for copy-initialization"
25505 " of %qT, as it has no non-explicit deduction guides or "
25506 "user-declared constructors", type);
25507 return error_mark_node;
25508 }
25509 }
25510 }
25511
25512 ++cp_unevaluated_operand;
25513 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25514 tf_decltype);
25515
25516 if (t == error_mark_node && (complain & tf_warning_or_error))
25517 {
25518 error ("class template argument deduction failed:");
25519 t = build_new_function_call (cands, &args, /*koenig*/false,
25520 complain | tf_decltype);
25521 if (old_cands != cands)
25522 inform (input_location, "explicit deduction guides not considered "
25523 "for copy-initialization");
25524 }
25525
25526 --cp_unevaluated_operand;
25527 release_tree_vector (args);
25528
25529 return TREE_TYPE (t);
25530}
25531
25532/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25533 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25534
25535tree
25536do_auto_deduction (tree type, tree init, tree auto_node)
25537{
25538 return do_auto_deduction (type, init, auto_node,
25539 tf_warning_or_error,
25540 adc_unspecified);
25541}
25542
25543/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25544 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25545 The CONTEXT determines the context in which auto deduction is performed
25546 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25547 OUTER_TARGS are used during template argument deduction
25548 (context == adc_unify) to properly substitute the result, and is ignored
25549 in other contexts.
25550
25551 For partial-concept-ids, extra args may be appended to the list of deduced
25552 template arguments prior to determining constraint satisfaction. */
25553
25554tree
25555do_auto_deduction (tree type, tree init, tree auto_node,
25556 tsubst_flags_t complain, auto_deduction_context context,
25557 tree outer_targs, int flags)
25558{
25559 tree targs;
25560
25561 if (init == error_mark_node)
25562 return error_mark_node;
25563
25564 if (init && type_dependent_expression_p (init)
25565 && context != adc_unify)
25566 /* Defining a subset of type-dependent expressions that we can deduce
25567 from ahead of time isn't worth the trouble. */
25568 return type;
25569
25570 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25571 /* C++17 class template argument deduction. */
25572 return do_class_deduction (type, tmpl, init, flags, complain);
25573
25574 if (TREE_TYPE (init) == NULL_TREE)
25575 /* Nothing we can do with this, even in deduction context. */
25576 return type;
25577
25578 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25579 with either a new invented type template parameter U or, if the
25580 initializer is a braced-init-list (8.5.4), with
25581 std::initializer_list<U>. */
25582 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25583 {
25584 if (!DIRECT_LIST_INIT_P (init))
25585 type = listify_autos (type, auto_node);
25586 else if (CONSTRUCTOR_NELTS (init) == 1)
25587 init = CONSTRUCTOR_ELT (init, 0)->value;
25588 else
25589 {
25590 if (complain & tf_warning_or_error)
25591 {
25592 if (permerror (input_location, "direct-list-initialization of "
25593 "%<auto%> requires exactly one element"))
25594 inform (input_location,
25595 "for deduction to %<std::initializer_list%>, use copy-"
25596 "list-initialization (i.e. add %<=%> before the %<{%>)");
25597 }
25598 type = listify_autos (type, auto_node);
25599 }
25600 }
25601
25602 if (type == error_mark_node)
25603 return error_mark_node;
25604
25605 init = resolve_nondeduced_context (init, complain);
25606
25607 if (context == adc_decomp_type
25608 && auto_node == type
25609 && init != error_mark_node
25610 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25611 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25612 and initializer has array type, deduce cv-qualified array type. */
25613 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25614 complain);
25615 else if (AUTO_IS_DECLTYPE (auto_node))
25616 {
25617 bool id = (DECL_P (init)
25618 || ((TREE_CODE (init) == COMPONENT_REF
25619 || TREE_CODE (init) == SCOPE_REF)
25620 && !REF_PARENTHESIZED_P (init)));
25621 targs = make_tree_vec (1);
25622 TREE_VEC_ELT (targs, 0)
25623 = finish_decltype_type (init, id, tf_warning_or_error);
25624 if (type != auto_node)
25625 {
25626 if (complain & tf_error)
25627 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25628 return error_mark_node;
25629 }
25630 }
25631 else
25632 {
25633 tree parms = build_tree_list (NULL_TREE, type);
25634 tree tparms;
25635
25636 if (flag_concepts)
25637 tparms = extract_autos (type);
25638 else
25639 {
25640 tparms = make_tree_vec (1);
25641 TREE_VEC_ELT (tparms, 0)
25642 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25643 }
25644
25645 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25646 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25647 DEDUCE_CALL, LOOKUP_NORMAL,
25648 NULL, /*explain_p=*/false);
25649 if (val > 0)
25650 {
25651 if (processing_template_decl)
25652 /* Try again at instantiation time. */
25653 return type;
25654 if (type && type != error_mark_node
25655 && (complain & tf_error))
25656 /* If type is error_mark_node a diagnostic must have been
25657 emitted by now. Also, having a mention to '<type error>'
25658 in the diagnostic is not really useful to the user. */
25659 {
25660 if (cfun && auto_node == current_function_auto_return_pattern
25661 && LAMBDA_FUNCTION_P (current_function_decl))
25662 error ("unable to deduce lambda return type from %qE", init);
25663 else
25664 error ("unable to deduce %qT from %qE", type, init);
25665 type_unification_real (tparms, targs, parms, &init, 1, 0,
25666 DEDUCE_CALL, LOOKUP_NORMAL,
25667 NULL, /*explain_p=*/true);
25668 }
25669 return error_mark_node;
25670 }
25671 }
25672
25673 /* Check any placeholder constraints against the deduced type. */
25674 if (flag_concepts && !processing_template_decl)
25675 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25676 {
25677 /* Use the deduced type to check the associated constraints. If we
25678 have a partial-concept-id, rebuild the argument list so that
25679 we check using the extra arguments. */
25680 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25681 tree cargs = CHECK_CONSTR_ARGS (constr);
25682 if (TREE_VEC_LENGTH (cargs) > 1)
25683 {
25684 cargs = copy_node (cargs);
25685 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25686 }
25687 else
25688 cargs = targs;
25689 if (!constraints_satisfied_p (constr, cargs))
25690 {
25691 if (complain & tf_warning_or_error)
25692 {
25693 switch (context)
25694 {
25695 case adc_unspecified:
25696 case adc_unify:
25697 error("placeholder constraints not satisfied");
25698 break;
25699 case adc_variable_type:
25700 case adc_decomp_type:
25701 error ("deduced initializer does not satisfy "
25702 "placeholder constraints");
25703 break;
25704 case adc_return_type:
25705 error ("deduced return type does not satisfy "
25706 "placeholder constraints");
25707 break;
25708 case adc_requirement:
25709 error ("deduced expression type does not satisfy "
25710 "placeholder constraints");
25711 break;
25712 }
25713 diagnose_constraints (input_location, constr, targs);
25714 }
25715 return error_mark_node;
25716 }
25717 }
25718
25719 if (processing_template_decl && context != adc_unify)
25720 outer_targs = current_template_args ();
25721 targs = add_to_template_args (outer_targs, targs);
25722 return tsubst (type, targs, complain, NULL_TREE);
25723}
25724
25725/* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25726 result. */
25727
25728tree
25729splice_late_return_type (tree type, tree late_return_type)
25730{
25731 if (is_auto (type))
25732 {
25733 if (late_return_type)
25734 return late_return_type;
25735
25736 tree idx = get_template_parm_index (type);
25737 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25738 /* In an abbreviated function template we didn't know we were dealing
25739 with a function template when we saw the auto return type, so update
25740 it to have the correct level. */
25741 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25742 }
25743 return type;
25744}
25745
25746/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25747 'decltype(auto)' or a deduced class template. */
25748
25749bool
25750is_auto (const_tree type)
25751{
25752 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25753 && (TYPE_IDENTIFIER (type) == auto_identifier
25754 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25755 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25756 return true;
25757 else
25758 return false;
25759}
25760
25761/* for_each_template_parm callback for type_uses_auto. */
25762
25763int
25764is_auto_r (tree tp, void */*data*/)
25765{
25766 return is_auto_or_concept (tp);
25767}
25768
25769/* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25770 a use of `auto'. Returns NULL_TREE otherwise. */
25771
25772tree
25773type_uses_auto (tree type)
25774{
25775 if (type == NULL_TREE)
25776 return NULL_TREE;
25777 else if (flag_concepts)
25778 {
25779 /* The Concepts TS allows multiple autos in one type-specifier; just
25780 return the first one we find, do_auto_deduction will collect all of
25781 them. */
25782 if (uses_template_parms (type))
25783 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25784 /*visited*/NULL, /*nondeduced*/true);
25785 else
25786 return NULL_TREE;
25787 }
25788 else
25789 return find_type_usage (type, is_auto);
25790}
25791
25792/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25793 'decltype(auto)' or a concept. */
25794
25795bool
25796is_auto_or_concept (const_tree type)
25797{
25798 return is_auto (type); // or concept
25799}
25800
25801/* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25802 a concept identifier) iff TYPE contains a use of a generic type. Returns
25803 NULL_TREE otherwise. */
25804
25805tree
25806type_uses_auto_or_concept (tree type)
25807{
25808 return find_type_usage (type, is_auto_or_concept);
25809}
25810
25811
25812/* For a given template T, return the vector of typedefs referenced
25813 in T for which access check is needed at T instantiation time.
25814 T is either a FUNCTION_DECL or a RECORD_TYPE.
25815 Those typedefs were added to T by the function
25816 append_type_to_template_for_access_check. */
25817
25818vec<qualified_typedef_usage_t, va_gc> *
25819get_types_needing_access_check (tree t)
25820{
25821 tree ti;
25822 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25823
25824 if (!t || t == error_mark_node)
25825 return NULL;
25826
25827 if (!(ti = get_template_info (t)))
25828 return NULL;
25829
25830 if (CLASS_TYPE_P (t)
25831 || TREE_CODE (t) == FUNCTION_DECL)
25832 {
25833 if (!TI_TEMPLATE (ti))
25834 return NULL;
25835
25836 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25837 }
25838
25839 return result;
25840}
25841
25842/* Append the typedef TYPE_DECL used in template T to a list of typedefs
25843 tied to T. That list of typedefs will be access checked at
25844 T instantiation time.
25845 T is either a FUNCTION_DECL or a RECORD_TYPE.
25846 TYPE_DECL is a TYPE_DECL node representing a typedef.
25847 SCOPE is the scope through which TYPE_DECL is accessed.
25848 LOCATION is the location of the usage point of TYPE_DECL.
25849
25850 This function is a subroutine of
25851 append_type_to_template_for_access_check. */
25852
25853static void
25854append_type_to_template_for_access_check_1 (tree t,
25855 tree type_decl,
25856 tree scope,
25857 location_t location)
25858{
25859 qualified_typedef_usage_t typedef_usage;
25860 tree ti;
25861
25862 if (!t || t == error_mark_node)
25863 return;
25864
25865 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25866 || CLASS_TYPE_P (t))
25867 && type_decl
25868 && TREE_CODE (type_decl) == TYPE_DECL
25869 && scope);
25870
25871 if (!(ti = get_template_info (t)))
25872 return;
25873
25874 gcc_assert (TI_TEMPLATE (ti));
25875
25876 typedef_usage.typedef_decl = type_decl;
25877 typedef_usage.context = scope;
25878 typedef_usage.locus = location;
25879
25880 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25881}
25882
25883/* Append TYPE_DECL to the template TEMPL.
25884 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25885 At TEMPL instanciation time, TYPE_DECL will be checked to see
25886 if it can be accessed through SCOPE.
25887 LOCATION is the location of the usage point of TYPE_DECL.
25888
25889 e.g. consider the following code snippet:
25890
25891 class C
25892 {
25893 typedef int myint;
25894 };
25895
25896 template<class U> struct S
25897 {
25898 C::myint mi; // <-- usage point of the typedef C::myint
25899 };
25900
25901 S<char> s;
25902
25903 At S<char> instantiation time, we need to check the access of C::myint
25904 In other words, we need to check the access of the myint typedef through
25905 the C scope. For that purpose, this function will add the myint typedef
25906 and the scope C through which its being accessed to a list of typedefs
25907 tied to the template S. That list will be walked at template instantiation
25908 time and access check performed on each typedefs it contains.
25909 Note that this particular code snippet should yield an error because
25910 myint is private to C. */
25911
25912void
25913append_type_to_template_for_access_check (tree templ,
25914 tree type_decl,
25915 tree scope,
25916 location_t location)
25917{
25918 qualified_typedef_usage_t *iter;
25919 unsigned i;
25920
25921 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25922
25923 /* Make sure we don't append the type to the template twice. */
25924 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25925 if (iter->typedef_decl == type_decl && scope == iter->context)
25926 return;
25927
25928 append_type_to_template_for_access_check_1 (templ, type_decl,
25929 scope, location);
25930}
25931
25932/* Convert the generic type parameters in PARM that match the types given in the
25933 range [START_IDX, END_IDX) from the current_template_parms into generic type
25934 packs. */
25935
25936tree
25937convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25938{
25939 tree current = current_template_parms;
25940 int depth = TMPL_PARMS_DEPTH (current);
25941 current = INNERMOST_TEMPLATE_PARMS (current);
25942 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25943
25944 for (int i = 0; i < start_idx; ++i)
25945 TREE_VEC_ELT (replacement, i)
25946 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25947
25948 for (int i = start_idx; i < end_idx; ++i)
25949 {
25950 /* Create a distinct parameter pack type from the current parm and add it
25951 to the replacement args to tsubst below into the generic function
25952 parameter. */
25953
25954 tree o = TREE_TYPE (TREE_VALUE
25955 (TREE_VEC_ELT (current, i)));
25956 tree t = copy_type (o);
25957 TEMPLATE_TYPE_PARM_INDEX (t)
25958 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25959 o, 0, 0, tf_none);
25960 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25961 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25962 TYPE_MAIN_VARIANT (t) = t;
25963 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25964 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25965 TREE_VEC_ELT (replacement, i) = t;
25966 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25967 }
25968
25969 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25970 TREE_VEC_ELT (replacement, i)
25971 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25972
25973 /* If there are more levels then build up the replacement with the outer
25974 template parms. */
25975 if (depth > 1)
25976 replacement = add_to_template_args (template_parms_to_args
25977 (TREE_CHAIN (current_template_parms)),
25978 replacement);
25979
25980 return tsubst (parm, replacement, tf_none, NULL_TREE);
25981}
25982
25983/* Entries in the decl_constraint hash table. */
25984struct GTY((for_user)) constr_entry
25985{
25986 tree decl;
25987 tree ci;
25988};
25989
25990/* Hashing function and equality for constraint entries. */
25991struct constr_hasher : ggc_ptr_hash<constr_entry>
25992{
25993 static hashval_t hash (constr_entry *e)
25994 {
25995 return (hashval_t)DECL_UID (e->decl);
25996 }
25997
25998 static bool equal (constr_entry *e1, constr_entry *e2)
25999 {
26000 return e1->decl == e2->decl;
26001 }
26002};
26003
26004/* A mapping from declarations to constraint information. Note that
26005 both templates and their underlying declarations are mapped to the
26006 same constraint information.
26007
26008 FIXME: This is defined in pt.c because garbage collection
26009 code is not being generated for constraint.cc. */
26010
26011static GTY (()) hash_table<constr_hasher> *decl_constraints;
26012
26013/* Returns the template constraints of declaration T. If T is not
26014 constrained, return NULL_TREE. Note that T must be non-null. */
26015
26016tree
26017get_constraints (tree t)
26018{
26019 if (!flag_concepts)
26020 return NULL_TREE;
26021
26022 gcc_assert (DECL_P (t));
26023 if (TREE_CODE (t) == TEMPLATE_DECL)
26024 t = DECL_TEMPLATE_RESULT (t);
26025 constr_entry elt = { t, NULL_TREE };
26026 constr_entry* found = decl_constraints->find (&elt);
26027 if (found)
26028 return found->ci;
26029 else
26030 return NULL_TREE;
26031}
26032
26033/* Associate the given constraint information CI with the declaration
26034 T. If T is a template, then the constraints are associated with
26035 its underlying declaration. Don't build associations if CI is
26036 NULL_TREE. */
26037
26038void
26039set_constraints (tree t, tree ci)
26040{
26041 if (!ci)
26042 return;
26043 gcc_assert (t && flag_concepts);
26044 if (TREE_CODE (t) == TEMPLATE_DECL)
26045 t = DECL_TEMPLATE_RESULT (t);
26046 gcc_assert (!get_constraints (t));
26047 constr_entry elt = {t, ci};
26048 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26049 constr_entry* entry = ggc_alloc<constr_entry> ();
26050 *entry = elt;
26051 *slot = entry;
26052}
26053
26054/* Remove the associated constraints of the declaration T. */
26055
26056void
26057remove_constraints (tree t)
26058{
26059 gcc_assert (DECL_P (t));
26060 if (TREE_CODE (t) == TEMPLATE_DECL)
26061 t = DECL_TEMPLATE_RESULT (t);
26062
26063 constr_entry elt = {t, NULL_TREE};
26064 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26065 if (slot)
26066 decl_constraints->clear_slot (slot);
26067}
26068
26069/* Memoized satisfaction results for declarations. This
26070 maps the pair (constraint_info, arguments) to the result computed
26071 by constraints_satisfied_p. */
26072
26073struct GTY((for_user)) constraint_sat_entry
26074{
26075 tree ci;
26076 tree args;
26077 tree result;
26078};
26079
26080/* Hashing function and equality for constraint entries. */
26081
26082struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26083{
26084 static hashval_t hash (constraint_sat_entry *e)
26085 {
26086 hashval_t val = iterative_hash_object(e->ci, 0);
26087 return iterative_hash_template_arg (e->args, val);
26088 }
26089
26090 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26091 {
26092 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26093 }
26094};
26095
26096/* Memoized satisfaction results for concept checks. */
26097
26098struct GTY((for_user)) concept_spec_entry
26099{
26100 tree tmpl;
26101 tree args;
26102 tree result;
26103};
26104
26105/* Hashing function and equality for constraint entries. */
26106
26107struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26108{
26109 static hashval_t hash (concept_spec_entry *e)
26110 {
26111 return hash_tmpl_and_args (e->tmpl, e->args);
26112 }
26113
26114 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26115 {
26116 ++comparing_specializations;
26117 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26118 --comparing_specializations;
26119 return eq;
26120 }
26121};
26122
26123static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26124static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26125
26126/* Search for a memoized satisfaction result. Returns one of the
26127 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26128
26129tree
26130lookup_constraint_satisfaction (tree ci, tree args)
26131{
26132 constraint_sat_entry elt = { ci, args, NULL_TREE };
26133 constraint_sat_entry* found = constraint_memos->find (&elt);
26134 if (found)
26135 return found->result;
26136 else
26137 return NULL_TREE;
26138}
26139
26140/* Memoize the result of a satisfication test. Returns the saved result. */
26141
26142tree
26143memoize_constraint_satisfaction (tree ci, tree args, tree result)
26144{
26145 constraint_sat_entry elt = {ci, args, result};
26146 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26147 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26148 *entry = elt;
26149 *slot = entry;
26150 return result;
26151}
26152
26153/* Search for a memoized satisfaction result for a concept. */
26154
26155tree
26156lookup_concept_satisfaction (tree tmpl, tree args)
26157{
26158 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26159 concept_spec_entry* found = concept_memos->find (&elt);
26160 if (found)
26161 return found->result;
26162 else
26163 return NULL_TREE;
26164}
26165
26166/* Memoize the result of a concept check. Returns the saved result. */
26167
26168tree
26169memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26170{
26171 concept_spec_entry elt = {tmpl, args, result};
26172 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26173 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26174 *entry = elt;
26175 *slot = entry;
26176 return result;
26177}
26178
26179static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26180
26181/* Returns a prior concept specialization. This returns the substituted
26182 and normalized constraints defined by the concept. */
26183
26184tree
26185get_concept_expansion (tree tmpl, tree args)
26186{
26187 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26188 concept_spec_entry* found = concept_expansions->find (&elt);
26189 if (found)
26190 return found->result;
26191 else
26192 return NULL_TREE;
26193}
26194
26195/* Save a concept expansion for later. */
26196
26197tree
26198save_concept_expansion (tree tmpl, tree args, tree def)
26199{
26200 concept_spec_entry elt = {tmpl, args, def};
26201 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26202 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26203 *entry = elt;
26204 *slot = entry;
26205 return def;
26206}
26207
26208static hashval_t
26209hash_subsumption_args (tree t1, tree t2)
26210{
26211 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26212 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26213 int val = 0;
26214 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26215 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26216 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26217 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26218 return val;
26219}
26220
26221/* Compare the constraints of two subsumption entries. The LEFT1 and
26222 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26223 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26224
26225static bool
26226comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26227{
26228 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26229 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26230 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26231 CHECK_CONSTR_ARGS (right1)))
26232 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26233 CHECK_CONSTR_ARGS (right2));
26234 return false;
26235}
26236
26237/* Key/value pair for learning and memoizing subsumption results. This
26238 associates a pair of check constraints (including arguments) with
26239 a boolean value indicating the result. */
26240
26241struct GTY((for_user)) subsumption_entry
26242{
26243 tree t1;
26244 tree t2;
26245 bool result;
26246};
26247
26248/* Hashing function and equality for constraint entries. */
26249
26250struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26251{
26252 static hashval_t hash (subsumption_entry *e)
26253 {
26254 return hash_subsumption_args (e->t1, e->t2);
26255 }
26256
26257 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26258 {
26259 ++comparing_specializations;
26260 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26261 --comparing_specializations;
26262 return eq;
26263 }
26264};
26265
26266static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26267
26268/* Search for a previously cached subsumption result. */
26269
26270bool*
26271lookup_subsumption_result (tree t1, tree t2)
26272{
26273 subsumption_entry elt = { t1, t2, false };
26274 subsumption_entry* found = subsumption_table->find (&elt);
26275 if (found)
26276 return &found->result;
26277 else
26278 return 0;
26279}
26280
26281/* Save a subsumption result. */
26282
26283bool
26284save_subsumption_result (tree t1, tree t2, bool result)
26285{
26286 subsumption_entry elt = {t1, t2, result};
26287 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26288 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26289 *entry = elt;
26290 *slot = entry;
26291 return result;
26292}
26293
26294/* Set up the hash table for constraint association. */
26295
26296void
26297init_constraint_processing (void)
26298{
26299 if (!flag_concepts)
26300 return;
26301
26302 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26303 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26304 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26305 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26306 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26307}
26308
26309/* Set up the hash tables for template instantiations. */
26310
26311void
26312init_template_processing (void)
26313{
26314 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26315 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26316}
26317
26318/* Print stats about the template hash tables for -fstats. */
26319
26320void
26321print_template_statistics (void)
26322{
26323 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26324 "%f collisions\n", (long) decl_specializations->size (),
26325 (long) decl_specializations->elements (),
26326 decl_specializations->collisions ());
26327 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26328 "%f collisions\n", (long) type_specializations->size (),
26329 (long) type_specializations->elements (),
26330 type_specializations->collisions ());
26331}
26332
26333#include "gt-cp-pt.h"
26334